本文主要是介绍vb.net三层实现登录例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看三层已经很长时间了,中间有经过了期末考试、回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大家分享一下先:
首先是界面:
这样感觉真心的好看
首先我们看一下实体层,也就是图片中的LoginModel
Public Class ModelDim _username As StringDim _password As StringDim _email As StringDim _score As IntegerPublic Property UserName As String '设置UserName属性GetReturn _usernameEnd GetSet(value As String)_username = valueEnd SetEnd PropertyPublic Property Password As String '设置Password属性GetReturn _passwordEnd GetSet(value As String)_password = valueEnd SetEnd PropertyPublic Property Email As StringGetReturn _emailEnd GetSet(value As String)_email = valueEnd SetEnd Property
D层
Public Class UserDAOPublic conn As New SqlConnection("server=李卫中; database=Login;user id=sa;password=li")Public Function SelectUser(ByVal User As Model) As LoginModel.ModelDim reader As SqlDataReaderDim eUser As New LoginModel.ModelDim 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语句的具体类型,在此为Selectcmd.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 WhileReturn 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.ModelDim eUser As New LoginModel.ModelDim 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语句的具体类型,在此为Selectcmd.Parameters.Add(New SqlParameter("@UserName", User.UserName))cmd.Parameters.Add(New SqlParameter("@Score", 10))conn.Open()cmd.ExecuteNonQuery()Return eUser '返回查询到的实体conn.Close() '视频里讲过,用完了之后,最好是断开连接,省的占用通道 '关闭连接End FunctionEnd Class
至于这里,写完之后,看到了慕夏师傅师傅的博客,知道了,应该向C#中一样,讲数据库连接字符串单独提出来,这样,就不用每次都写,见谅
U层:界面层
Public Class frmLoginPrivate Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.LoadtxtUserName.Focus()txtUserName.MaxLength = 11 '设置txtUserName 属性txtPassword.MaxLength = 10End SubPrivate Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.ClickTryDim euser2 As New LoginModel.Model '实例化新的UserInfo,用来传递B层的实体Dim euser3 As LoginModel.Model '定义一个类型为UserInfo的参数,用来赋值'用户名、密码传入实体层euser2.UserName = Trim(txtUserName.Text)euser2.Password = txtPassword.TextDim mgr As New LoginBLL.LoginManagereuser3 = mgr.UserLogin(euser2)Catch ex As ExceptionMessageBox.Show(ex.Message.ToString())End TryEnd SubPrivate Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.ClickMe.Close()End Sub
End Class
B层:业务逻辑层
Public Function UserLogin(ByVal User As LoginModel.Model) As LoginModel.ModelDim uDao As New LoginDAL.UserDAODim sDao As New LoginDAL.ScoresDAODim eUser1 As LoginModel.ModeleUser1 = uDao.SelectUser(User)If IsNothing(eUser1.UserName) ThenThrow New Exception("密码错误或用户名不存在,登录失败")ElseeUser1 = sDao.UpdateScore(User)Return eUser1End IfEnd Function
自己编点,看看师哥师姐们的博客,结合一下自己以前学到的知识,要知道,所谓的三层其实就是一个解耦的过程,和我们后面遇到的“七层”是一样,用心看看,应该不难
从上面可以看出,我用到的是传实体,之前我问过师傅,到底是传实体好呢,还是传参好呢?(各有各的好处,不同的地方、不同的情况用不同的发放)这是师傅对我说的,虽然我还不能完全掌握使用方法的能力,但是通过练习,我没问题
这篇关于vb.net三层实现登录例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!