学习三层有几天了,跟着视频中的步骤把c#版的三层登录实现了,看培养计划上说要实现VB.NET 版的,于是我就尝试着用VB.NET 实现三层登录。可事实总是想着容易,做起来难啊,虽然说VB.NET 跟VB差别不是太大吧,但是VB基本已经忘的差不多了,而且好多语句是不一样的,看VB.Net 视频感觉没有学到多少东西,要我自己照着c#版的敲代码,真的好难啊。。。经过我的调查,好多同学也都是经过参考完成的。就这样我也开始一步一步的完成我的项目。。。。。。
先说说用VB.NET和用c#语言的不同之处吧
1、引用命名空间:
VB.NET 中:用Improts 添加命名空间如:ImprotsSystem.Data.SqlClient
Imports System.Data
C# 中:用的是using添加命名空间,如:
using System.Data;
using System.Data.SqlClient
2、对象的初始化写法不同:
如U层中对B层的初始化:
Vb.net中:Dim userAs New login.Modle.UserInfo
C#中:Login.BLL.LoginManager mgr=new Login.BLL.LoginManager();
其实区别还是挺多的,在这里就不一一说明了,给大家一个链接感兴趣的可以看看:
http://blog.csdn.net/cooska/article/details/8490020
下面说在U、B、D这三层中的数据传递过程:
下面给大家展示一下详细的代码:
实体层:(封装、传递数据)
代码:
Public Class UserInfo Public _Id As Integer Public Property Id As Integer Get Return _Id End Get Set(value As Integer) _Id = value End Set End Property Public _UserName As String Public Property UserName As String Get Return _UserName End Get Set(value As String) _UserName = value End Set End Property Public _Password As String Public Property Password As String Get Return _Password End Get Set(value As String) _Password = value End Set End Property Public Email As String End Class
U层:获取输入的用户名和密码,初始化B层,将数据传入到B层中
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click Dim manager As New login.Bll.loginManager() Dim user As New login.Modle.UserInfo user.UserName = txtuser.Text.Trim() user.Password = txtpassword.Text If manager.SelectUserandBll(user) Then MsgBox("登录成功") Else MessageBox.Show("登录失败") End If End End Sub
B层:初始化D层中的对象,将数据传入到D层,并处理从D层返回来的数据。
Public Class loginManager '实例化D层和Modle层对象 Dim uDao As New login.DAL.UserDAO Dim Buser As New login.Modle.UserInfo Function SelectUserandBll(ByVal user As login.Modle.UserInfo) As Boolean Buser.UserName = user.UserName Buser = uDao.SelectUser(Buser) '引用D层。 If Buser.Password.Trim() = user.Password Then Return True Else Return False End If End Function End Class
D 层:建立与数据库的连接和查询语句,对数据库进行查询,将查询的结果返回给B层。
连接数据库:
Imports System.Data ‘添加引用 Imports System.Data.SqlClient Public Class UserDAO '登录数据库 Dim sqlConnectStr As String = "server=LU;database=Login;user id=sa;password=1" '查询数据库,查看用户是否存在 Public Function SelectUser(ByVal user As login.Modle.UserInfo) As login.Modle.UserInfo '链接数据库 Dim sql As String = "select*from Users where UserName='" & user.UserName & "'" 'connection连接数据库 Dim sqlConnect As SqlConnection = New SqlConnection(sqlConnectStr) sqlConnect.ConnectionString = sqlconnectStr 'command对象对数据库执行命名 Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnect) cmd.CommandText = sql '获得sql语句的具体内容 cmd.Connection = sqlConnect Dim reader As SqlDataReader '从数据源读取数据 Dim UserDataTable As New DataTable '定义一个datatable对象 Dim Duser As New login.Modle.UserInfo '定义model对象 Try sqlConnect.Open() '打开链接 reader = cmd.ExecuteReader '返回一个数据集对象 UserDataTable.Load(reader) Duser.UserName = UserDataTable.Rows(0)("UserName") Duser.Password = UserDataTable.Rows(0)("Password") Return Duser '返回实体对象 Catch ex As Exception Duser.Password = "" Return Duser Finally If Not IsNothing(sqlConnect) Then sqlConnect.Close() End If End Try End Function End Class
有了这些代码,就可以运行出来结果,完美的完成三层登录了!