本文主要是介绍登录实例对比-C#和VB.NET实现三层,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大家在阅读这篇文章之前可以先阅读另外一篇博文《三层结构》(请点击这里)。对其有最基本的认识。
在看视频的过程中,用C#实现了登录的实例,之后对其又转换为了VB.NET。实现功能是一样的,只是换汤不换药,换了一种表达方式而已。对比着来学习效果更佳。
对比一:实体层
C#实现:
<span style="font-size:18px;">public class UserInfo{public int ID { get; set; }public string UserName { get; set; }public string Password { get; set; }public string Email { get; set; }}</span>
VB.NET实现:
<span style="font-size:18px;">Public Class UserInfoPrivate _ID As IntegerPublic Property ID As IntegerGetReturn _IDEnd GetSet(value As Integer)_ID = valueEnd SetEnd PropertyPrivate _UserName As StringPublic Property UserName As StringGetReturn _UserNameEnd GetSet(value As String)_UserName = valueEnd SetEnd PropertyPrivate _Password As StringPublic Property Password As StringGetReturn _PasswordEnd GetSet(value As String)_Password = valueEnd SetEnd PropertyPrivate _Email As StringPublic Property Email As StringGetReturn _EmailEnd GetSet(value As String)_Email = valueEnd SetEnd Property
End Class</span>
对比分析:都是为了封装数据,便于数据在三层中流转,很明显,C#比VB.NET的代码简单很多,但实现的功能却是相同的。有一个最大的区别非常显眼,C#每句结尾都有分号,而VB.NET没有。
对比二:DAL层
C#实现:
<span style="font-size:18px;">class DbUtil{public static string ConnString = @"Server=qiwei;Database=Login; User ID=sa; Password=130427"; }</span>
<span style="font-size:18px;">public class UserDAO{public Login .Model .UserInfo SelectUser(string userName, string password){using(SqlConnection conn=new SqlConnection (DbUtil.ConnString )){SqlCommand cmd = conn.CreateCommand();cmd.CommandText = @"SELECT ID,UserName, Password, EmailFROM USERS 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();SqlDataReader reader = cmd.ExecuteReader();Login.Model.UserInfo user = null;while (reader.Read()){if (user == null){user = new Login.Model.UserInfo();}user.ID =reader.GetInt32(0);user.UserName = reader.GetString(1);user.Password = reader.GetString(2); //not suggestionif (!reader.IsDBNull(3)){user.Email = reader.GetString(3);}}return user;}}}</span>
VB.NET实现:
<span style="font-size:18px;">Public Class UserDAOPublic conn As New SqlConnection("server =localhost;database=Login;user=sa;password=130427")Dim sql As String = "select ID,UserName,Password,Email from Users where UserName=@UserName"Public Function SelectUser(ByVal userName As String, ByVal password As String)Dim cmd As New SqlCommand(sql, conn)Dim reader As SqlDataReaderDim user1 As New LoginEntity.UserInfo()cmd.CommandText = sqlcmd.CommandType = CommandType.Textcmd.Parameters.Add(New SqlParameter("@UserName", userName))cmd.Parameters.Add(New SqlParameter("@Password", password))conn.Open()reader = cmd.ExecuteReaderWhile reader.Read()If user1 Is Nothing Thenuser1 = New LoginEntity.UserInfo()End Ifuser1.ID = reader.GetInt32(0)user1.UserName = reader.GetString(1)user1.Password = reader.GetString(2)If Not reader.IsDBNull(3) Thenuser1.Email =reader .GetString (3)End IfEnd WhileReturn user1End Function
End Class</span>
对比分析:都是提供基本的数据访问,连接数据库,从数据库中查找数据,然后读数据,将数据传递到实体层。
C#中,先单独将连接数据类DbUtil放在一个类库,方便之后各类连接数据库调用,对今后若更换数据库提供方便。当然VB.NET同样可以实现。这里直接与数据库进行相连。
DAL层都需要引用实体层。用到数据库都需要添加引用,C#实现:
<span style="font-size:18px;">using System.Data;
using System.Data.SqlClient;</span>
VB.NET实现:
<span style="font-size:18px;">Imports System.Data
Imports System.Data.SqlClient</span>
如果仔细观察,C#中只有conn.Open(),打开数据库连接,而没有conn.Close(),关闭数据库连接。这里Using(){}函数体的使用是自动关闭的。对比三:BLL层
C#实现:
<span style="font-size:18px;">public class LoginManager{public Login.Model.UserInfo UserLogin(string userName, string password){Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();Login.Model.UserInfo user = uDao.SelectUser(userName, password);if (user != null) //login successfully{Login.DAL.ScoreDAO sDao = new Login.DAL.ScoreDAO();sDao.UpdateScore(userName, 10);return user;}else{throw new Exception("登录失败!");}}}</span>
VB.NET实现:
<span style="font-size:18px;">Public Class LoginMagnagerPublic Function UserLogin(ByVal userName As String, ByVal password As String)Dim uDAO As New LoginDAL.UserDAODim user1 As New LoginEntity.UserInfouser1 = uDAO.SelectUser(userName, password)If IsNothing(user1.UserName) ThenThrow New Exception("登录失败")ElseReturn user1End IfEnd Function
End Class</span>
对比分析:这里用VB.NET实现的时候没有加入积分的功能。两种代码的实现都是为了将DAL层的数据传递给实体层,然后进行逻辑判断。但代码的表现形式还是不一样的。
对比四:UI层
c#实现:
<span style="font-size:18px;">private void btnLogin_Click(object sender, EventArgs e){string userName = txtUserName.Text.Trim();string password = txtPassword.Text;Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();Login.Model .UserInfo user=mgr.UserLogin(userName, password); MessageBox.Show("登录用户:" + user.UserName); }</span>
VB.NET实现:
<span style="font-size:18px;">Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.ClickDim userName As StringDim password As StringDim mgr As New loginBLL.LoginMagnagerDim user As LoginEntity.UserInfouserName = txtUserName.Textpassword = txtPassword.Textuser = mgr.UserLogin(userName, password)MessageBox.Show("登录成功!用户名:" + userName)End Sub</span>
对比分析:都是采集用户的输入信息,然后将BLL层的数据传递给实体层。从这里可以看出最大的不同就是,C#边定义边使用,而VB.NET每个出现的数据都必须先定义,然后才能使用。
总结:
通过学习这个简单的登录实例,用C#和VB.NET去实现三层。首先,明确掌握了三层结构的应用。其次,又一次感受到了计算机语言的相同性。只有扎实地掌握了一门语言,再学习其它语言那就是照葫芦画瓢的事儿了,so easy!
这篇关于登录实例对比-C#和VB.NET实现三层的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!