看三层已经很长时间了,中间有经过了期末考试、回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大家分享一下先:
首先是界面:
这样感觉真心的好看
首先我们看一下实体层,也就是图片中的LoginModel
Public Class Model Dim _username As String Dim _password As String Dim _email As String Dim _score As Integer Public Property UserName As String '设置UserName属性 Get Return _username End Get Set(value As String) _username = value End Set End Property Public Property Password As String '设置Password属性 Get Return _password End Get Set(value As String) _password = value End Set End Property Public Property Email As String Get Return _email End Get Set(value As String) _email = value End Set End PropertyD层
Public Class UserDAO Public conn As New SqlConnection("server=李卫中; database=Login;user id=sa;password=li") Public Function SelectUser(ByVal User As Model) As LoginModel.Model Dim reader As SqlDataReader Dim eUser As New LoginModel.Model Dim sql As String = "Select UserName,Password From Users Where UserName=@UserName And Password=@Password" Dim cmd As New SqlCommand(sql, conn) '创建sqlCommand对象 cmd.CommandText = sql '获取SQL语句的具体内容 cmd.CommandType = CommandType.Text '获取上述SQL语句的具体类型,在此为Select cmd.Parameters.Add(New SqlParameter("@UserName", User.UserName)) '若用成eUser.UserName则会出现参数未传递的错误提示 cmd.Parameters.Add(New SqlParameter("@Password", User.Password)) conn.Open() '打开数据连接 reader = cmd.ExecuteReader() '执行查询语句,并生成一个DataReader '读取查询到的数据,并返回给相应的属性 While reader.Read() '获取数据库中相应字段的数据 '数组必须从零开始读取,否则会超出其界限 eUser.UserName = reader.GetString(0) eUser.Password = reader.GetString(1) End While Return eUser '返回查询到的实体 conn.Close() End Function
Public Class ScoresDAO '这里是实例中的那个登录见证,也就是Score那部分,其实我是照葫芦画瓢,照上面写的 Public conn As New SqlConnection("server=李卫中; database=Login;user id=sa;password=li") Public Function UpdateScore(ByVal User As Model) As LoginModel.Model Dim eUser As New LoginModel.Model Dim sql As String = "INSERT INTO Scores (UserName,Score) values (@UserName,@Score)" Dim cmd As New SqlCommand(sql, conn) '创建sqlCommand对象 cmd.CommandText = sql '获取SQL语句的具体内容 cmd.CommandType = CommandType.Text '获取上述SQL语句的具体类型,在此为Select cmd.Parameters.Add(New SqlParameter("@UserName", User.UserName)) cmd.Parameters.Add(New SqlParameter("@Score", 10)) conn.Open() cmd.ExecuteNonQuery() Return eUser '返回查询到的实体 conn.Close() '视频里讲过,用完了之后,最好是断开连接,省的占用通道 '关闭连接 End Function End Class至于这里,写完之后,看到了慕夏师傅师傅的博客,知道了,应该向C#中一样,讲数据库连接字符串单独提出来,这样,就不用每次都写,见谅
Public Class frmLogin Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load txtUserName.Focus() txtUserName.MaxLength = 11 '设置txtUserName 属性 txtPassword.MaxLength = 10 End Sub Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click Try Dim euser2 As New LoginModel.Model '实例化新的UserInfo,用来传递B层的实体 Dim euser3 As LoginModel.Model '定义一个类型为UserInfo的参数,用来赋值 '用户名、密码传入实体层 euser2.UserName = Trim(txtUserName.Text) euser2.Password = txtPassword.Text Dim mgr As New LoginBLL.LoginManager euser3 = mgr.UserLogin(euser2) 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 End ClassB层:业务逻辑层
Public Function UserLogin(ByVal User As LoginModel.Model) As LoginModel.Model Dim uDao As New LoginDAL.UserDAO Dim sDao As New LoginDAL.ScoresDAO Dim eUser1 As LoginModel.Model eUser1 = uDao.SelectUser(User) If IsNothing(eUser1.UserName) Then Throw New Exception("密码错误或用户名不存在,登录失败") Else eUser1 = sDao.UpdateScore(User) Return eUser1 End If End Function
从上面可以看出,我用到的是传实体,之前我问过师傅,到底是传实体好呢,还是传参好呢?(各有各的好处,不同的地方、不同的情况用不同的发放)这是师傅对我说的,虽然我还不能完全掌握使用方法的能力,但是通过练习,我没问题