三层登录VB.NET实现
用C#实现了一遍三层登录,然后再使用VB.NET实现,然后转为七层登录,七层登录成功后,直接开始机房的VB.NET个人重构。
具体的过程省略了,直接上代码:
界面层
Imports Login.Model Imports Login.BLL '显示层 Public Class UserLogin Private Sub btnLogin_click(sender As Object, e As EventArgs) Handles btnLogin.Click '定义实体层对象 Dim UIUserInfo As New Login.Model.User_Info '定义BLL层的对象 Dim BLLManager As New Login.BLL.LoginManager '将控件的输入信息赋值给实体层对象 UIUserInfo.UserID = txtUserName.Text() UIUserInfo.UserPassword = txtPassword.Text() '把UI层的信息,传给DLL层中的自定义函数 '通过自定义函数的处理结果的返回值,判断用户是否登录成功 If BLLManager.FunctionUI(UIUserInfo) Then MsgBox("登陆成功!") Else MsgBox("登录失败!") End If End Sub Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click End End Sub End Class
业务逻辑层
Imports Login.Model Imports Login.DAL.UserDAO '业务逻辑层 Public Class LoginManager '自定义函数,判断用户名和密码是否正确 Public Function FunctionUI(ByVal user As Login.Model.User_Info) As Boolean '实例化实体层对象 Dim BLLUserInfo As New Login.Model.User_Info '实例化DAL层对象 Dim DALUser As New Login.DAL.UserDAO '实体层对象,用来接受DAL层传来的数据 BLLUserInfo = DALUser.FunctionDAL(user) '判读从UI层和DAL层传来的数据是否相等,也就是用户名和密码是否相同 '通过在BLL层自定义函数的参数,把UI层用户输入的数据传到BLL层 '最后,在BLL层对数据进行处理,返回结果值 If BLLUserInfo.UserID.Trim = user.UserID.Trim And BLLUserInfo.UserPassword.Trim = user.UserPassword.Trim Then Return True Else Return False End If End Function End Class
数据访问层
'定义链接数据库类 Module DbUtil '定义链接数据库字符串 Public connString As String = "Server=liushaofeng;database=charge_sys;User ID=sa;Password=123456" End Module Imports System.Data.SqlClient Imports Login.Model '数据访问层 Public Class UserDAO '自定义函数,查询和接收数据库中的数据 Public Function FunctionDAL(User As Login.Model.User_Info) As Login.Model.User_Info '定义实体层对象,接收来自数据库的数据 Dim DALUserInfo As New Login.Model.User_Info '定义数据库记录集 '定义一个读取器 '若要创建 SqlDataReader,必须调用 SqlCommand 对象的 ExecuteReader 方法,而不要直接使用构造函数。 Dim conn As New SqlConnection(DbUtil.connString) Dim reader As SqlDataReader '具体的数据查询语句 Dim sql As String = "Select UserID,PWD From User_Info Where UserID=@UserName And PWD=@Password" Dim cmd As New SqlCommand(sql, conn) '获取SQL语句的具体内容 '获取上述SQL语句的具体类型,在此为Select cmd.CommandText = sql cmd.CommandType = CommandType.Text '添加命令参数 cmd.Parameters.Add(New SqlParameter("@UserName", User.UserID)) cmd.Parameters.Add(New SqlParameter("@Password", User.UserPassword)) '打开数据连接 '执行查询语句,并生成一个DataReader conn.Open() reader = cmd.ExecuteReader() '读取查询到的数据,并返回给相应的属性 While reader.Read() '获取数据库中相应字段的数据 DALUserInfo.UserID = reader.GetString(0).ToString DALUserInfo.UserPassword = reader.GetString(1) End While '返回查询到的实体 '关闭连接 Return DALUserInfo conn.Close() End Function End Class
实体层
<span style="font-size:24px;">'实体层 Public Class User_Info '定义实体类UserInfo '声明数据库中字段信息 Private _UserID As String Public Property UserID As String '读数据 Get Return _UserID End Get '写数据 Set(value As String) _UserID = value End Set End Property Private _UserPassword As String Public Property UserPassword As String Get Return _UserPassword End Get Set(value As String) _UserPassword = value End Set End Property End Class</span>