before you read this article, you can read another blog post. "Three-storey structure" (please click here). Have the most basic understanding of it.
In the process of watching video, we implemented the instance of login in C #, and then converted it to vb.net. Realize the function is the same, just the new, change a way of expression. Compare to learn better.
Contrast one: Physical layer
C # implementations:
<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 implementation:
<span style= "font-size:18px;" >public Class UserInfo Private _id As Integer public property ID As Integer Get Return _id End Get Set (value as Integer) _id = value End Set End Property Private _username as Stri Ng Public Property UserName As String get Return _username End Get Set (value as String) _username = value End Set End Property Private _password as String public property Password as String Get Return _password End Get Set (value as String) _password = value End Set End Property Private _email As String public property Email As String Get Return _e Mail End Get Set (value as String) _email = value End Set End Propertyend Class</span
Comparative Analysis: all in order to encapsulate data, easy data flow in three layers, it is obvious that C # than VB. NET code is much simpler, but the functionality of the implementation is the same. One of the biggest differences is very conspicuous, and C # has a semicolon at the end of each sentence, and vb.net does not.
contrast two: the DAL layer
C # implementations:
<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 (DbU Til. connstring)) {SqlCommand cmd = conn. CreateCommand (); Cmd. CommandText = @ "Select Id,username, Password, Email from the USERS WHERE [Email protec Ted] and [email protected] "; 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 suggestion if (!reader. IsDBNull (3)) {User. Email = reader. GetString (3); }} return user; }}}</span> VB. NET implementation:
<span style= "FONT-SIZE:18PX;" >public Class Userdao Public conn as New SqlConnection ("Server =localhost;database=login;user=sa;password=130427") Dim SQL as String = "Select Id,username,password,email from the Users where [email protected]" public Function Selec TUser (ByVal userName As String, ByVal Password As String) Dim cmd As New SqlCommand (SQL, conn) Dim reader As SqlDataReader Dim user1 as New loginentity.userinfo () cmd. CommandText = sql cmd. CommandType = CommandType.Text cmd. Parameters.Add (New SqlParameter ("@UserName", UserName)) cmd. Parameters.Add (New SqlParameter ("@Password", Password)) Conn. Open () reader = cmd. ExecuteReader while reader. Read () If User1 is Nothing then User1 = New loginentity.userinfo () End if User1.id = reader. GetInt32 (0) user1. UserName = reader. GetString (1) User1. Password = reader. GetString (2) IfNot reader. IsDBNull (3) then User1. Email =reader. GetString (3) End If End while Return user1 End functionend class</span> Comparative Analysis: both provide basic data access, connect to the database, look up data from the database, and then read the data, passing the data to the entity layer.
C #, the connection data class Dbutil is placed in a class library, convenient after the various types of connection database calls, in the future if the replacement of the database to provide convenience. Of course vb.net can also be achieved. This is directly connected to the database.
The DAL layer is required to reference the entity layer. Use the database to add references, C # implementations:
<span style= "FONT-SIZE:18PX;" >using system.data;using system.data.sqlclient;</span>
VB. NET implementation:
<span style= "FONT-SIZE:18PX;" >imports System.dataimports system.data.sqlclient</span>
If you look closely, there is only Conn in C #. Open (), opens the database connection without Conn. Open () to close the database connection. The use of the using () {} function body is automatically closed here.
contrast three: the BLL layer
C # implementations:
<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, ten); return user; } else { throw new Exception ("Login failed! "); } } } </span>
VB. NET implementation:
<span style= "FONT-SIZE:18PX;" >public Class loginmagnager public Function userlogin (ByVal userName As String, ByVal password as String) Dim u DAO as new Logindal.userdao Dim user1 as new loginentity.userinfo user1 = udao.selectuser (userName, password)
if IsNothing (user1. UserName) then Throw New Exception ("Login Failed") Else Return user1 end If End FunctionEnd class</ Span>
Comparative Analysis: here, when implemented with VB.net, does not include integral functions. Both codes are implemented to pass data from the DAL layer to the entity layer and then make logical judgments. But the code does not behave the same way.
contrast four: UI layer
C # implementations:
<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 ("Logged in User:" + user.) UserName); } </span>
VB. NET implementation:
<span style= "FONT-SIZE:18PX;" >private Sub Btnlogin_click (sender as Object, e as EventArgs) Handles Btnlogin.click Dim userName As String Dim Password as String Dim mgr As New loginbll.loginmagnager Dim user As Loginentity.userinfo userName = Txtusern Ame. Text password = txtpassword.text user = Mgr. Userlogin (userName, password) MessageBox.Show ("Login Successful! User name: "+ userName" End sub</span>
Comparative Analysis: All of the user input information is collected, and then the data of the BLL layer is passed to the physical layer. The biggest difference you can see here is that the C # side defines the edges to use, and vb.net each occurrence of the data must be defined before it can be used.
Summary:
By learning this simple login instance, use C # and vb.net to achieve layer three. First, the application of the three-layer structure is clearly mastered. Second, I feel the similarity of computer language once again. Only a solid grasp of a language, and then learn other languages that is divert thing, so easy!
Login instance Compare-c# and vb.net implementation layer three