结束设计模式,紧接着就是vb.net的视频,但是没怎么理解,然后就是三层登录了,因为设计模式是一个一个敲的,对C#语言稍微懂一点点,但是vb.net没有实战过,一点思路也没用,前两天用c#语言实现了三层的登录,想试着用vb.net实现,但是不知道怎样分层,查了很多博客,搜了一些资料终于也实现了,其实vb.net和c#也超不多,就是照猫画虎,依照葫芦画瓢,感觉vb.net在vb和c#直接,既有和vb相似的地方也有和C#一样之处。
下面是三层登录窗体vb.net的实现版。
【UI层】
<span style="font-family:KaiTi_GB2312;font-size:18px;"> Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Try
Dim User As New Login.Entity.UserInfo '定义一个新的实体,用来存放用户名和密
<span style="font-family:KaiTi_GB2312;"> </span>User.UserName = txtUserName.Text.Trim '存放用户名
User.Password = txtPassword.Text '密码
Dim mgr As New Login.BLL.LoginManager '实例化新的BLL,调用BLL层类LoginManager中的方法UserLogin
User = mgr.UserLogin(User)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString()) '出错时系统提示
End Try
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click '退出
Me.Close()
End Sub</span>
【BLL层】
<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class LoginManager
Public Function UserLogin(ByVal User As Login.Entity.UserInfo) As Login.Entity.UserInfo
Dim uDao As New Login.DAL.UserDAO '定义新的DAO
Dim eUser1 As Login.Entity.UserInfo '定义实体层
eUser1 = uDao.SelectUser(User) '调用DAO中的方法SelectUser
If IsNothing(eUser1.UserName) Then
Throw New Exception("登录失败,请检查输入的用户名和密码")
Else
MsgBox("登录成功,正在进入系统……")
Return eUser1
End If
End Function
End Class</span>
【DAL层】
<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class UserDAO
'创建数据连接
Public conn As New SqlConnection("Server=LFH;Database=LoginSanCeng;User ID=sa;Password=1")
Public Function SelectUser(ByVal User As UserInfo) As Entity.UserInfo
Dim reader As SqlDataReader '定义reader,从SQL Server数据库读取行的只进流的方式
Dim eUser As New Login.Entity.UserInfo '定义实体
'Dim sql As String = "Select UserName,Password from User where UserName=@UserName and Password=@Password" '查询语句
Dim sql As String = "Select UserName,Password from [dbo].[User] where UserName=@UserName and Password=@Password" '查询语句
Dim cmd As New SqlCommand(sql, conn) '数据库的执行语句
cmd.CommandText = sql
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New SqlParameter("@UserName", User.UserName))
cmd.Parameters.Add(New SqlParameter("@Password", User.Password)) '出现问题 ???只传入一个参数,上接UI层的问题
conn.Open()
reader = cmd.ExecuteReader()
While reader.Read()
eUser.UserName = reader.GetString(0)
eUser.Password = reader.GetString(1)
End While
<span style="font-family:KaiTi_GB2312;"> </span>Return eUser
conn.Close()
End Function
End Class
</span>
【实体层】
<span style="font-family:KaiTi_GB2312;font-size:18px;">Private _username As String '用户名字段
Public Property UserName As String '通过字段赋值属性
Get‘读取数据
Return _username
End Get
Set(value As String)’修改数据
_username = value
End Set
End Property
Private _password As String '密码
Public Property Password As String
Get
Return _password
End Get
Set(value As String)
_password = value
End Set
End Property</span>
调试中的遇见的问题
1、传参数的问题
<span style="font-family:KaiTi_GB2312;font-size:18px;"> 'Dim User2 As New Login.Entity.UserInfo’不可以定义两个,分别传值,因为最后是两个参数的用户名和密码
'Dim User3As New Login.Entity.UserInfo‘这样的话User2只有一个参数用户名
'User2.UserName = txtUserName.Text.Trim
'User3.Password = txtPassword.Text
'Dim mgr As New Login.BLL.LoginManager
'User3 = mgr.UserLogin(User2)
'Dim sql As String = "Select UserName,Password from [dbo].[User] where UserName=@UserName and Password=@Password" '查询语句
'cmd.Parameters.Add(New SqlParameter("@UserName", User.UserName))
'cmd.Parameters.Add(New SqlParameter("@Password", User.Password))
</span>
原因:定义两个实体分别赋值,传进去的只是一个User2,但是最后需要两个UserName和Password,所以导致出现问题,因此只需定义一个实体通过属性赋值就好了
2msgbox使用问题
<span style="font-family:KaiTi_GB2312;font-size:18px;">'MsgBox("登录成功,正在进入系统……""恭喜") '用法错误</span>
msgbox使用有三个参数
Prompt As Object——必选,显示的内容
Buttons As MsgBoxStyle——可选,显示的按钮,默认为0
Title As Object——可选,弹出框的标题
许多事情在没有开始时可能是我们想的太难,只要大胆去尝试,一切都可以解决,需要学习的还有很多,加油吧!