经过一周多的学习基本了解三层,但是敲代码的时候不知道如何驾驭,这两天一直在敲登陆实例,不过终于有点眉目了。
首先新建解决方案,添加类库和windows窗体应用程序,添加引用,然后设计窗体和写代码
1.实体层
根据数据库中字段编写实体层代码
Public Class UserModel
Private LoginID As String '字段
Private LoginPWD As String
Private Name As String
Public Property _LoginID As String '属性
Get
Return LoginID
End Get
Set(ByVal value As String)
LoginID = value
End Set
End Property
Public Property _LoginPWD As String
Get
Return LoginPWD
End Get
Set(ByVal value As String)
LoginPWD = value
End Set
End Property
Public Property _Name As String
Get
Return Name
End Get
Set(ByVal value As String)
Name = value
End Set
End Property
End Class
2、数据访问层
连接数据库并实现增删改查的功能
Imports System.Data.SqlClient
Imports My.Model
Public Class UserDAL
Public Shared Function connstring() As String '连接数据库
connstring = "Data Source=192.168.24.47;Initial Catalog=MyPro;uid=sa;Pwd=123456;"
End Function
''' <summary>
''' 查询数据
''' </summary>
''' <param name="strloginid">用户名</param>
''' <param name="strloginpwd">密码</param>
''' <returns>实体类</returns>
''' <remarks></remarks>
Public Function Query(ByVal strloginid As String, ByVal strloginpwd As String) As UserModel
Dim sqlconnection1 As SqlClient.SqlConnection '执行数据库连接
Dim cmd As SqlClient.SqlCommand '使用command对象执行命令并返回
sqlconnection1 = New SqlClient.SqlConnection(UserDAL.connstring())
cmd = sqlconnection1.CreateCommand() '创建插入命令
cmd.CommandText = "select * from User_Info where LoginID=@loginid and LoginPWD=@loginpwd"
cmd.CommandType = CommandType.Text
'参数化查询防sql注入
cmd.Parameters.Add(New SqlClient.SqlParameter("@loginid", strloginid))
cmd.Parameters.Add(New SqlClient.SqlParameter("@loginpwd", strloginpwd))
sqlconnection1.Open() '打开连接
Dim reader As SqlDataReader '定义一次查询
reader = cmd.ExecuteReader() '执行查询
Dim userModel As Model.UserModel
userModel = Nothing
While (reader.Read()) '读取txt中的内容
If userModel Is Nothing Then
userModel = New Model.UserModel
End If
userModel._LoginID = reader.GetString(0) '将第一个数据转换为字符串型然后赋给实体层的User
userModel._LoginPWD = reader.GetString(1)
End While
Return userModel '返回查询结果
End Function
End Class
3、业务逻辑层
Imports My.DAL
Imports My.Model
Public Class UserBLL
''' <summary>
''' 判断用户登录
''' </summary>
''' <param name="strloginid">用户名</param>
''' <param name="strloginpwd">密码</param>
''' <returns>实体层</returns>
''' <remarks></remarks>
Public Function UserLogin(ByVal strloginid As String, ByVal strloginpwd As String) As UserModel
Dim DBDAL As New DAL.UserDAL '实例化数据访问层
Dim userModel As New Model.UserModel '实例化实体层
userModel = DBDAL.Query(strloginid, strloginpwd) '根据参数确定返回值
If (userModel Is Nothing) Then
MsgBox("登陆失败")
Else
MsgBox("登录成功")
End If
Return userModel
End Function
End Class
4、表现层
不能出现任何逻辑
Public Class LoginUI
''' <summary>
''' 点击登录,将消息显示在界面
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnLogin_Click(sender As Object, e As EventArgs)
Dim strLoginID As String
Dim strLoginPwd As String
strLoginID = Trim(txtLoginID.Text)
strLoginPwd = Trim(txtLoginPwd.Text)
Dim userBLL As New Bll.UserBLL '实例化业务逻辑层
Dim userModel As New Model.UserModel
userModel = userBLL.UserLogin(strLoginID, strLoginPwd) '引用业务逻辑层传参
txtLoginID.Text = " "
txtLoginPwd.Text = " "
End Sub
End Class
三层的登录实例就实现了,欢迎大家指正