初接触三层
- 三层是指显示层,业务逻辑层,数据访问层,是为“高内聚,低耦合”服务的
- 除了上述三层之外,一个程序中必须要有的还有实体层,我的理解是实体层中的实体是与数据库中的表相对的,而实体的属性是与数据表中字段相对的,大家还可以根据需要加入外观层,数据接口,抽象工厂层这都是后话,要根据程序的需要进行添加。就像我们今天要实现的登陆这个就不需要外观,数据接口,抽象共厂,其实就连业务逻辑层也可以省略,因为这里并没有逻辑运算。
- 显示层引用业务逻辑层,业务逻辑层引用数据访问层,三层均可根据需要引用实体层
- 最初敲登陆这个小例子,犯了一个小错误,就是
- 这个问题花了我一晚上的时间,也花了我旁边大神半个小时,但都无果。就在我要心灰意冷,将要咬舌自尽之时,我忽然注意到:
显示层
Imports LogIn.BLL Imports LogIn.Model Public Class frmLogin Public Sub btnLogIn_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnLogIn.Click Dim admininfo As AdminInfoEntity Dim manager As New adminBLL Dim _name As String Dim _password As String _name = txtAdminName.Text _password = txtAdminPwd.Text admininfo = manager.selectAdmin_info(_name,_password) '调用逻辑层,返回的admininfo可能为空,也可能不为空 If admininfo Is Nothing Then MessageBox.Show("登陆失败") Else MessageBox.Show("登陆用户" + admininfo.Name) End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnCancel.Click Me.Close() End Sub End Class业务逻辑层
Imports LogIn.Model Imports LogIn.DAL Public Class adminBLL Dim AdminInfo As AdminInfoEntity Dim AdminDAL As New AdminDAL '验证用户登录信息是否正确 Public Function selectAdmin_info(ByVal name As String,ByVal password As String) As AdminInfoEntity AdminInfo = AdminDAL.getAdminInfo(name,password) '调用数据访问层,返回的admininfo可能为空,也可能不为空 Return AdminInfo End Function End Class数据访问层
Imports System.Data.sqlClient Imports System.Data Imports System.Data.OleDb Imports System Public Class ConnectionToDB Public Function ConnectionToDB() As sqlConnection Dim con As New sqlConnection("Data Source=刘颖-PC;Initial Catalog=Room_Charge_System;User ID=sa;password=123456") '连接数据库 con.Open() Return con End Function End Class
Imports System Imports System.Data Imports System.Data.OleDb Imports System.Data.sqlClient Imports LogIn.Model Public Class AdminDAL Dim AdminInfo As AdminInfoEntity Dim con As New ConnectionToDB '得到用户信息 Public Function getAdminInfo(ByVal name As String,ByVal password As String) As AdminInfoEntity '查表中的记录 Dim cmd As New sqlCommand("select*from T_Admin_info where Name=" & "'" & name & "'" & "and Password=" & "'" & password & "'",con.ConnectionToDB()) Dim reader As sqlDataReader reader = cmd.ExecuteReader() While (reader.Read()) '如果数据表中有用户名和密码都与文本框对应的记录,则利用new 关键字调用AdminInfoEntity构造函数,使admininfo不为空,并对其属性赋值 AdminInfo = New AdminInfoEntity AdminInfo.ID = reader.GetString(0) AdminInfo.Name = reader.GetString(1) AdminInfo.Level = reader.GetString(2) AdminInfo.Password = reader.GetString(3) End While Return AdminInfo End Function End Class实体层
'用户类,分别提供了其他类可以调用的公有属性,也提供了供类内部成员调用的字段,其他类通过属性可以限制对字段的无条件进行操作,例如只读,只写,限定范围赋值。 Public Class AdminInfoEntity Dim _name As String Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Dim _id As String Public Property ID() As String Get Return _id End Get Set(ByVal value As String) _id = value End Set End Property Dim _level As String Public Property Level() As String Get Return _level End Get Set(ByVal value As String) _level = value End Set End Property Dim _password As String Public Property Password() As String Get Return _password End Get Set(ByVal value As String) _password = value End Set End Property End Class原文链接:https://www.f2er.com/vb/257961.html