本文主要是介绍【VB.NET机房重构】.NET三层登录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
重构这么久,刚摸出点门道,先来介绍一下.NET三层登录。
一、分析
1、逻辑分析
2、前提准备
3、登录界面
二、代码实现
Entity层代码(UserInfoEntity和WorkLogEntity)
Public Class UserInfoEntity'定义变量Private struserID As StringPrivate strpassword As StringPrivate strlevel As StringPrivate strstatus As StringPublic Property UserID() As StringGetReturn struserIDEnd GetSet(value As String)struserID = valueEnd SetEnd PropertyPublic Property Password() As StringGetReturn strpasswordEnd GetSet(value As String)strpassword = valueEnd SetEnd PropertyPublic Property Level() As StringGetReturn strlevelEnd GetSet(value As String)strlevel = valueEnd SetEnd PropertyPublic Property Status() As StringGetReturn strstatusEnd GetSet(value As String)strstatus = valueEnd SetEnd Property
End Class
Public Class WorkLogEntityPrivate struserID As StringPrivate strloginTime As StringPrivate strlogoutTime As StringPrivate strstatus As StringPublic Property UserID() As StringGetReturn struserIDEnd GetSet(value As String)struserID = valueEnd SetEnd PropertyPublic Property LoginTime() As StringGetReturn strloginTimeEnd GetSet(value As String)strloginTime = valueEnd SetEnd PropertyPublic Property LogoutTime() As StringGetReturn strlogoutTimeEnd GetSet(value As String)strlogoutTime = valueEnd SetEnd PropertyPublic Property Status() As StringGetReturn strstatusEnd GetSet(value As String)strstatus = valueEnd SetEnd Property
End Class
DAL层代码(UserInfoDAL和WorkLogDAL)
Imports System.Data.SqlClient
Imports System.Data
Public Class UserInfoDAL''' <summary>''' 查询用户''' </summary>''' <param name="userinfoentity1"></param>''' <returns></returns>''' <remarks></remarks>Function SelectUser(ByVal userinfoentity1 As Entity.UserInfoEntity)Dim conn As New SqlConnection '创建连接对象Dim cmd As New SqlCommand '创建命令对象conn = New SqlConnection(Dbutil.connstring()) '连接数据库cmd.Connection = conn '获取SQL语句的内容Dim reader As SqlDataReader '定义SqlDataReader的变量reader'传递数据cmd.Parameters.Add(New SqlParameter("@UserID", userinfoentity1.UserID))cmd.Parameters.Add(New SqlParameter("@Password", userinfoentity1.Password))'定义SQL语句cmd.CommandText = "Select * from UserInfo where UserID=@UserID and Password=@Password and Status='True'"cmd.CommandType = CommandType.Textconn.Open() '打开数据库链接reader = cmd.ExecuteReader() '执行查询语句,生成一个DataReaderDim userinfoentity3 As New Entity.UserInfoEntity'读取查询到的数据,返回相应属性While reader.Read()userinfoentity3.UserID = reader.GetString(0)userinfoentity3.Password = reader.GetString(1)End WhileReturn userinfoentity3End Function
End Class
Imports System.Data.SqlClient
Imports System.Data
Public Class WorkLogDALPublic Function InsertUser(ByVal worklogentity As Entity.WorkLogEntity) As BooleanDim conn As New SqlConnection '创建连接对象Dim cmd As New SqlCommand '创建命令对象conn = New SqlConnection(Dbutil.connstring()) '连接数据库cmd.Connection = conn'传入数据cmd.Parameters.Add(New SqlParameter("@UserID", Entity.Common2.strCurrentUserID))cmd.Parameters.Add(New SqlParameter("@Status", True))worklogentity.LoginTime = Format(Now, "yyyy-mm-dd hh:mm:ss")cmd.Parameters.Add(New SqlParameter("@LoginTime", worklogentity.LoginTime))'定义SQL语句cmd.CommandText = "Insert into WorkLog(UserID,LoginTime,Status)values(@UserID,@LoginTime,@Status)"conn.Open()Dim num As Integer '表示插入命令的结果Dim flag As New Booleannum = cmd.ExecuteNonQuery '执行插入命令If num > 0 Thenflag = TrueElseflag = FalseEnd IfReturn flagEnd Function
End Class
BLL层代码(LoginBLL和AddWorkLogBLL)
Public Class LoginBLLPublic Function UserLogin(ByVal userinfoentity2 As Entity.UserInfoEntity) As Entity.UserInfoEntityDim userinfodal As New DAL.UserInfoDAL '实例化UserInfoDALDim userinfoentity4 As Entity.UserInfoEntity '定义一个UserInfoEntity的参数userinfoentity4 = userinfodal.SelectUser(userinfoentity2)'判断是否查询到记录If IsNothing(userinfoentity4.UserID) ThenMsgBox("登录失败,用户名和密码不正确!")ElseMsgBox("登录成功,进入系统!")End IfReturn userinfoentity4End Function
End Class
Public Class AddWorkLogBLLPublic Function AddWorkLog(ByVal worklogentity As Entity.WorkLogEntity) As BooleanDim worklogdal As New DAL.WorkLogDAL '实例化WorkLogDALDim flag As Boolean 'flag用于定义DAL层的方法WorkLog的返回结果flag = worklogdal.InsertUser(worklogentity)Return flagEnd Function
End Class
UI层代码
Public Class frmLoginPrivate Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.ClickDim userinfoentity As New Entity.UserInfoEntity '实例化UserInfoEntityDim worklogeneity As New Entity.WorkLogEntity '实例化WorkLogEntityDim userinfoentity5 As Entity.UserInfoEntity '定义一个实体层的参数'将界面中的数据传给实体层userinfoentity.UserID = txtUserID.Text.Trim()userinfoentity.Password = txtPassword.Text.Trim()'判断输入框是否为空If txtUserID.Text = "" ThenMessageBox.Show("用户名不能为空")ReturnEnd IfIf txtPassword.Text = "" ThenMessageBox.Show("密码不能为空")ReturnEnd If'调用B层,进行判断Dim loginbll As New BLL.LoginBLL '实例化LoginBLLuserinfoentity5 = loginbll.UserLogin(userinfoentity)'将正在登录的用户登录信息传给全局变量Dim strcommon As New Entity.Common2strcommon.CurrentUserID = txtUserID.Text.Trim()strcommon.CurrentPassword = txtPassword.Text.Trim()Dim flag As BooleanDim addworklogbll As New BLL.AddWorkLogBLL '实例化AddWorkLogBLLflag = addworklogbll.AddWorkLog(worklogeneity)Me.Visible = FalsefrmMain.Visible = TrueEnd Sub
End Class
运行结果:
总结:三层虽然很简单,但是也要认真的搞清楚,这是后面重构的基础。不管会不会,一定要不停的先去做,再不行先照着别人的敲出来,搞清楚各层的调用和跳转再自己重新敲,肯定会有收获。
这篇关于【VB.NET机房重构】.NET三层登录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!