在做三层demo之前,先用VB.NET做了一个一层的demo,数据库的连接及对其操作和业务判断的代码都写在了窗体里面,相对容易,但是随着业务的复杂化,处理的数据需要一定的存储层时便引来了三层的概念,因为显示层不能和数据源打交道,便有了业务逻辑层来支持,我们学习的三层主要指逻辑上的,包括UI、BLL、DAL,当然还有实体层。然后实现了一下视频中的c#登录,跟VB.NET有些语法差别,有些混乱。
一、我理解的三层
使用三层主要是解耦和,使复杂问题简单化,更好的体现面向对象思想。其中UI就是一个界面,是服务请求方,采集用户的信息和指令并展现特定的业务数据,它引用BLL中的登录服务方法,然后将其数据封装在数据模型User中;BLL是业务逻辑层,是对显示层的服务,将请求转给DAL层,引用D层中的对用户选择的方法,并将数据传给数据模型User,以User中是否为空来判断登录的状态,当然也通过DAL写入数据源;DAL是提供服务的,就和数据源打交道,执行增删改查的操作;最后就是实体层,它实现对数据的封装访问,其他三层都来引用它,在三个层次之间流转、传输数据,而不知道外面的其他三层存在。
二、如何实现
1、建立数据库
2、各层代码
(1)UI层
Imports System.Data.sqlClient Imports Login.BLL.LoginService Imports Login.Model Public Class Form1 Private Sub cmdOK_Click(sender As Object,e As EventArgs) Handles cmdOK.Click Dim UserName As String Dim password As String Dim sve As Login.BLL.LoginService '定义B层服务LoginService的对象 Dim user As Login.Model.UserInfo '定义数据模型中UserInfo的对象 UserName = txtUserName.Text.Trim() password = txtPWD.Text.Trim() sve = New Login.BLL.LoginService() '引用B层服务,并将数据信息赋值给数据模型user user = sve.UserLogin(UserName,password) MsgBox("登录成功") End Sub
(2)BLL层
Imports Login.DAL Imports Login.Model Imports System.Data.sqlClient Public Class LoginService Public Function UserLogin(ByVal userName As String,ByVal password As String) Dim user As Login.Model.UserInfo Dim uDAO As Login.DAL.UserDAO uDAO = New Login.DAL.UserDAO user = uDAO.SlectUser(userName,password) If user Is nothing Then MsgBox("登录失败") Else MsgBox("登录成功" + userName) Return user End If End Function End Class
(3)DAL
Imports System Imports System.Data Imports System.Data.OleDb Imports System.Data.sqlClient Public Class DbUtil Public ConnString As String = "data source=llp-PC;initial catalog=Login;uid=sa;password=123456" End Class
Imports System Imports System.Data Imports System.Data.OleDb Imports System.Data.sqlClient Imports Login.Model Public Class UserDAO Dim user As UserInfo Public Function SlectUser(ByVal userName As String,ByVal password As String) Dim db As New Login.DAL.DbUtil Using conn As New sqlConnection(db.ConnString) Dim cmd As New sqlCommand cmd = conn.CreateCommand() cmd.CommandText = "select * from [User] where UserName=@UserName And [Password]=@Password" cmd.CommandType = CommandType.Text cmd.Parameters.Add(New sqlParameter("@userName",userName)) cmd.Parameters.Add(New sqlParameter("@password",password)) conn.open() Dim read As sqlDataReader read = cmd.ExecuteReader() user = nothing While (read.Read()) If (user Is nothing) Then user = New Login.Model.UserInfo() End If user.ID = read.GetString(0) user.Password = read.GetString(1) user.UserName = read.GetString(2) End While Return user End Using End Function End Class
(4)实体层
Public Class UserInfo Public _userName As String Public Property UserName() As String Get Return _userName End Get Set(ByVal value As String) _userName = value End Set End Property Public _id As String Public Property ID() As String Get Return _id End Get Set(ByVal value As String) _id = value End Set End Property Public _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
三、遇到问题
1、未找到元数据文件
解决:新建项目时应原则类库,之前都是选择控制台,因为不涉及到三层中的引用,控制台不可产生DLL,没有动态链接库,所以出现U层无法引用实体和B层中数据,找不到元数据文件。
2、不支持关键字userid
3、数据库连接时出现与网络相关的或特定于实例的错误,未找到或无法访问服务器
4、关键字User附近有错误
5、遗留问题