VB.NET版+三层实现登陆
三层已经学了一些时间了,開始认为自己能够用C#敲代码了,就用C#写了一个实现登陆的,真正再用在机房中,还是认为非常吃力的,所以,决定用vb.net敲了。以下是我用vb.net实现的登陆,能够给大家做一些參考。
这是构架的三层
数据库中的表T_User_DAL:UserI,Level,Password,Head,computer
首先看一下
Entity层:
Public Class UserInfoEntity #Region "定义变量" Private _userID As String Private _level As String Private _Head As String Private _password As String #End Region Public Property UserID() As String ‘将T_User_DAL表中的每个实体都传上去,实体是依据表来建的,不是功能 Get Return _userID End Get Set(value As String) _userID = value End Set End Property Public Property Level() As String Get Return _level End Get Set(ByVal value As String) _level = value End Set End Property Public Property Head() As String Get Return _Head End Get Set(value As String) _Head = value End Set End Property Public Property Password() As String Get Return _password End Get Set(value As String) _password = value End Set End Property End Class
DAL层:
为了可以减少耦合一定要将连接字符串拿出来,不要每个DAL中都写一边。
Imports System.Data Imports System.Data.SqlClient Public Class SqlUtil Public Shared Function connstring() As String ‘connstring = "Server=192.168.24.183;Database=ReconsitutionCharge_sys;User ID=sa;Password=123456" ‘会出现无法识别userid的错误 connstring = "Server=mx; Database=ReconsitutionCharge_sys; User ID=sa; Password=123456" ‘连接SQL的字符串 End Function End Class
T_User_dal表DAL层代码:
Imports System.Data.SqlClient Imports System.Data Public Class UserDAL Public Function SelectUser(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity Dim conn As New SqlConnection ‘定义连接打开数据库 Dim cmd As New SqlCommand ‘定义数据库命令 conn = New SqlConnection(SqlUtil.connstring()) ‘实例化SQLUntil中返回的字符串 cmd.Connection = conn cmd.CommandText = "Select * From T_User_DAL Where UserID=@UserID and Password=@Password" ‘存储过程 cmd.Parameters.Add(New SqlParameter("@UserID", user.UserID)) ‘赋值,传參 cmd.Parameters.Add(New SqlParameter("@Password", user.Password)) cmd.CommandType = CommandType.Text ‘CommandText 属性设置为存储过程的名称。当调用 Execute 方法之中的一个时,该命令将运行此存储过程 conn.Open() ‘打开连接 Dim reader As SqlClient.SqlDataReader ‘读取数据库中的表 reader = cmd.ExecuteReader() Dim users As New Entity.UserInfoEntity ‘实例化实体 While (reader.Read()) ‘If users Is Nothing Then ‘ user = New Entity.UserInfoEntity ‘End If users.UserID = reader.GetString(reader.GetOrdinal("UserID")) users.Password = reader.GetString(reader.GetOrdinal("Password")) End While conn.Close() Return users End Function End Class
BLL层:
Public Class LoginBLL Function UserLogin(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity Dim uDAL As New DAL.UserDAL ‘实例化了DAL中的UserDAL Dim users As New Entity.UserInfoEntity ‘实例化了Entity层信息表 users = uDAL.SelectUser(user) ‘selectUser是返回一个Entity类的,给它赋值user Return users ‘返回users实体 End Function End Class
UI层:
界面设计:
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim mgr As New BLL.LoginBLL ‘实例业务层
Dim user As New Entity.UserInfoEntity ‘实例实体
Dim users As Entity.UserInfoEntity ‘接收实体,用于接收UserLogin()返回的数据类型,假设没有New就不是实例化
user.UserID = txtUserID.Text.Trim() ‘给实体层传入值
user.Password = TxtPassword.Text.Trim()
If txtUserID.Text = "" Then ‘推断输入是否正确
MsgBox("username不能为空!")
Return
End If
If TxtPassword.Text = "" Then
MsgBox("password不能为空!")
Return
End If
Try ‘users接收实体
users = mgr.UserLogin(user)
If (users.UserID Is Nothing And users.Password Is Nothing) Then ‘假设users不为空,则登陆成功!
MsgBox("登陆失败,username和password不匹配")
Return
Else
MsgBox("登陆成功!登陆用户:" + user.UserID)
End If
Catch ex As Exception
MsgBox(ex.Message())
End Try
End Sub
End Class
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。