六月份已经过去11天了,着手vb.net也好几天了,但是仿佛看到机房收费有一种莫名的恐惧和兴奋,说道前面的三层只不过那是用C#编写的,但是我们的机房使用vb.net编写,所以我就用vb编写了关于登陆界面的代码。
下面来看看U层代码:
Public Class Form1 Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click '实例化一个ui层用到的实体类 Dim Uuser As New Mobel.LoginMobel '将输入的内容,赋给实例化对象 Uuser.userName = txtUserName.Text Uuser.passWord = txtPassword.Text If BLL.LoginBLL.isnullLoginBLL(Uuser) Then MsgBox("登录成功") Else MsgBox("登录失败") End If End Sub End Class接着是实体层代码:
Public Class LoginMobel Private MuserName As String Public Property userName() As String Get Return MuserName End Get Set(value As String) MuserName = value End Set End Property Private MpassWord As String Public Property passWord() As String Get Return MpassWord End Get Set(value As String) MpassWord = value End Set End Property End Class下面是B层代码:
Public Class LoginBLL Public Shared Function isnullLoginBLL(ByVal UtextUser As Mobel.LoginMobel) As String Dim DtextUser As Mobel.LoginMobel Dim BtextUser As New DAL.LoginDAL DtextUser = BtextUser.LoginDALSelectUser(UtextUser.userName) If DtextUser Is Nothing Then Return False End If If UtextUser.passWord = DtextUser.passWord Then Return True Else Return False End If End Function End Clas
最后是D层代码:
Imports System.Data.SqlClient Public Class LoginDAL Public Function LoginDALSelectUser(ByVal UserName As String) As Mobel.LoginMobel '建立数据库连接字符串 Dim LoginConn As New SqlClient.SqlConnection("server=XZP-PC;database=Login;User ID=sa ;Password=123456") '建立数据库命令字符串 Dim LoginCmm As New SqlClient.SqlCommand("SELECT UserName,Password FROM Users") '打开数据库 LoginConn.Open() '定义数据库库读取方式 Dim LoginReader As SqlDataReader '读取数据库中的内容 LoginReader = LoginCmm.ExecuteReader Dim textUser As New Mobel.LoginMobel If LoginReader.Read() Then textUser.userName = LoginReader.GetFieldValue(Of String)(0) textUser.passWord = Trim(LoginReader.GetFieldValue(Of String)(1)) Return textUser LoginReader.Close() LoginConn.Close() Else Return Nothing End If End Function End Class在添加窗体引用时,出现了一点点的错误,让BLL,UI,DAL,MOBEL互相引用了,出现了下面的错误
所以在添加引用方面要理解好三层之间的关系,才能做到万无一失!