重构这么久,刚摸出点门道,先来介绍一下.NET三层登录。
一、分析
1、逻辑分析
2、前提准备
3、登录界面
二、代码实现
Entity层代码(UserInfoEntity和WorkLogEntity)
Public Class UserInfoEntity '定义变量 Private struserID As String Private strpassword As String Private strlevel As String Private strstatus As String Public Property UserID() As String Get Return struserID End Get Set(value As String) struserID = value End Set End Property Public Property Password() As String Get Return strpassword End Get Set(value As String) strpassword = value End Set End Property Public Property Level() As String Get Return strlevel End Get Set(value As String) strlevel = value End Set End Property Public Property Status() As String Get Return strstatus End Get Set(value As String) strstatus = value End Set End Property End Class
Public Class WorkLogEntity Private struserID As String Private strloginTime As String Private strlogoutTime As String Private strstatus As String Public Property UserID() As String Get Return struserID End Get Set(value As String) struserID = value End Set End Property Public Property LoginTime() As String Get Return strloginTime End Get Set(value As String) strloginTime = value End Set End Property Public Property LogoutTime() As String Get Return strlogoutTime End Get Set(value As String) strlogoutTime = value End Set End Property Public Property Status() As String Get Return strstatus End Get Set(value As String) strstatus = value End Set End Property End ClassDAL层代码(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.Text conn.Open() '打开数据库链接 reader = cmd.ExecuteReader() '执行查询语句,生成一个DataReader Dim userinfoentity3 As New Entity.UserInfoEntity '读取查询到的数据,返回相应属性 While reader.Read() userinfoentity3.UserID = reader.GetString(0) userinfoentity3.Password = reader.GetString(1) End While Return userinfoentity3 End Function End Class
Imports System.Data.SqlClient Imports System.Data Public Class WorkLogDAL Public Function InsertUser(ByVal worklogentity As Entity.WorkLogEntity) As Boolean Dim 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 Boolean num = cmd.ExecuteNonQuery '执行插入命令 If num > 0 Then flag = True Else flag = False End If Return flag End Function End ClassBLL层代码(LoginBLL和AddWorkLogBLL)
Public Class LoginBLL Public Function UserLogin(ByVal userinfoentity2 As Entity.UserInfoEntity) As Entity.UserInfoEntity Dim userinfodal As New DAL.UserInfoDAL '实例化UserInfoDAL Dim userinfoentity4 As Entity.UserInfoEntity '定义一个UserInfoEntity的参数 userinfoentity4 = userinfodal.SelectUser(userinfoentity2) '判断是否查询到记录 If IsNothing(userinfoentity4.UserID) Then MsgBox("登录失败,用户名和密码不正确!") Else MsgBox("登录成功,进入系统!") End If Return userinfoentity4 End Function End Class
Public Class AddWorkLogBLL Public Function AddWorkLog(ByVal worklogentity As Entity.WorkLogEntity) As Boolean Dim worklogdal As New DAL.WorkLogDAL '实例化WorkLogDAL Dim flag As Boolean 'flag用于定义DAL层的方法WorkLog的返回结果 flag = worklogdal.InsertUser(worklogentity) Return flag End Function End ClassUI层代码
Public Class frmLogin Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click Dim userinfoentity As New Entity.UserInfoEntity '实例化UserInfoEntity Dim worklogeneity As New Entity.WorkLogEntity '实例化WorkLogEntity Dim userinfoentity5 As Entity.UserInfoEntity '定义一个实体层的参数 '将界面中的数据传给实体层 userinfoentity.UserID = txtUserID.Text.Trim() userinfoentity.Password = txtPassword.Text.Trim() '判断输入框是否为空 If txtUserID.Text = "" Then MessageBox.Show("用户名不能为空") Return End If If txtPassword.Text = "" Then MessageBox.Show("密码不能为空") Return End If '调用B层,进行判断 Dim loginbll As New BLL.LoginBLL '实例化LoginBLL userinfoentity5 = loginbll.UserLogin(userinfoentity) '将正在登录的用户登录信息传给全局变量 Dim strcommon As New Entity.Common2 strcommon.CurrentUserID = txtUserID.Text.Trim() strcommon.CurrentPassword = txtPassword.Text.Trim() Dim flag As Boolean Dim addworklogbll As New BLL.AddWorkLogBLL '实例化AddWorkLogBLL flag = addworklogbll.AddWorkLog(worklogeneity) Me.Visible = False frmMain.Visible = True End Sub End Class运行结果:
总结:三层虽然很简单,但是也要认真的搞清楚,这是后面重构的基础。不管会不会,一定要不停的先去做,再不行先照着别人的敲出来,搞清楚各层的调用和跳转再自己重新敲,肯定会有收获。