(1) 向用户展示特定业务数据
(2) 采集用户的输入信息和操作
Public Class frmLoginPrivate Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click'定义实例Dim en_User As New Entity.en_UserInfoDim bl_User As New BLL.bl_Login'赋值en_User.UserID = txtUserName.Text.Trimen_User.Password = txtPassword.Text.Trim'采集用户信息,展示登录结果If bl_User.LoginManager(en_User) ThenMsgBox("登陆成功")ElseMsgBox("登录失败")End IfEnd SubPrivate Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.ClickMe.Close()End Sub
End Class
(1) 从DAL获取数据,以供UI显示
(2) 从UI获取用户指令和数据,执行业务逻辑
Imports Entity
Imports DAL.dal_LoginPublic Class bl_LoginPublic Function LoginManager(ByVal User As Entity.en_UserInfo) As BooleanDim dal_User As New DAL.dal_LoginDim en_User As New Entity.en_UserInfoen_User.UserID = User.UserID'调用D层的方法'en_User = dal_User.selectUser(en_User)en_User = dal_User.selectUser(User)'判断操作If en_User.UserID.Trim = User.UserID And en_User.Password.Trim = User.Password ThenReturn TrueElseReturn FalseEnd IfEnd Function
End Class
(1) 属于哪一层很难界定,比较倾向于业务逻辑层,也可以是数据访问层
(2) 目的:为了封装数据的,数据为了在三个层次之间流畅的流转
(3) 独立于其他三个层次的,不会引用任何的层次,其他三层都需要引用实体层
Imports EntityPublic Class en_UserInfo'声明UserID属性Private e_UserID As StringPublic Property UserID As StringGetReturn e_UserIDEnd GetSet(ByVal value As String)e_UserID = valueEnd SetEnd Property'声明Password属性Private e_Password As StringPublic Property Password As StringGetReturn e_PasswordEnd GetSet(ByVal value As String)e_Password = valueEnd SetEnd Property
End Class
4. DAL层
作用主要是与操作数据库
(1) 从数据源加载数据(select)
(2) 向数据源写入数据(insert/update)
(3) 从数据源删除数据(delete)
Imports Entity
Imports System.Data.SqlClient
Imports System.DataPublic Class dal_Login'连接数据库Dim strConn As String = "server =192.168.24.59;database=Login;uid=sa;pwd=123456"Dim sqlConnect As SqlConnection = New SqlConnection(strConn)'自定义检查参数Function selectUser(ByVal User As Entity.en_UserInfo) As Entity.en_UserInfoDim read As SqlDataReaderDim en_User As New Entity.en_UserInfo'其实下面的这段程序就相当于dim sql as string ="select ID,UserName,PWD From Users Where UserName='"User.UserName"' And PWD='"User.PWD"'"'而写成这个程序块是为了防止Sql注入,即安全性考虑。'@UserName相当于传了一个参数,("@UserName", User.UserName)相当于给参数名字传递了参数。Dim sql As String = "Select UserID,Password From UserInfo Where UserID=@UserID And Password=@Password"Dim sqlCmd As SqlCommand = New SqlCommand(sql, sqlConnect) '创建sqlCommand对象 'Dim UserDataTable As New DataTable '定义一个DataTable对象sqlCmd.CommandText = sql '获取SQL语句的具体内容sqlCmd.CommandType = CommandType.Text '获取上述SQL语句的具体类型,在此为SelectsqlCmd.Parameters.Add(New SqlParameter("@UserID", User.UserID)) '若用成eUser.UserName则会出现参数未传递的错误提示sqlCmd.Parameters.Add(New SqlParameter("@Password", User.Password))sqlConnect.Open()read = sqlCmd.ExecuteReader() '执行查询语句,并生成一个DataReaderread.Read()'读取查询到的数据,并返回给相应的属性While read.Read() '获取数据库中相应字段的数据'数组必须从零开始读取,否则会超出其界限User.UserID = read.GetString(0)User.Password = read.GetString(1)End While'如果用户存在的话,将数据库表中检索的记录对应赋值给参数'User.UserID = read.Item("UserID")'User.Password = read.Item("Password")Return User '返回查询到的实体sqlConnect.Close()End FunctionEnd Class