Because before the machine room reconstruction can not start, so master gave me the advice is to try to knock three layer of increase, delete, change, check, room reconstruction is mostly around the operation of the database to expand. As it turns out, master is right. Now knock three layer login and on this basis knocked three layers of additions and deletions, immediately feel the machine room reconstruction Enlightened, hey!
As a result of the three-tier login and deletion of these four pieces of content is indeed too much. After some "pondering", decided to divide these contents into four pieces: increase, delete, change, check.
The following first introduce to you "increase":
The first is to set up the database, as it was before, no longer explained.
Then the code is written, as follows:
Physical Layer:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" > ' <summary> ' entity layer, storing multiple properties ' </summary> ' ' <remarks></remarks>public Class User ' Defines the UserName property Private _username As String public property UserName As String Get Return _username End Get Set (value as String) _username = value End Set End Property ' Define password attribute Private _ PassWord As String ' public property PassWord As String get Return _password End Get Set (value As String) _password = value end Set end Propertyend class</span>
U-layer:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >public Class Form1 ' query Private Sub btnlogin_click (sender as Object, e as EventArgs) Handles btnselect.click
try ' Gets the data for the presentation layer Dim Muser as new Loginmodel.user ' instantiates a new entity to pass the entity muser of Layer B . UserName = TxtUserName.Text.Trim ' passes the user name to the UserName Muser of the entity layer . PassWord = TxtPassWord.Text.Trim ' Pass the password to the entity layer PassWord ' call layer B, login to determine Dim Mgr as New LOGINBLL.SELECTBLL Muser = Mgr. SELECTBLL (Muser) Catch ex as Exception MessageBox.Show (ex. Message.tostring ()) ' Error handling end Try End SubEnd class</span>
b Layer:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" > SELECTBLL public Function SELECTBLL (ByVal User as Loginmodel.user) as Loginmodel.user Dim Udao as New Logindal.sqlserveruser () ' instantiates a new Userdao object in the D layer Dim Muser as Loginmodel.user ' defines a type as the entity layer parameter, the user assigns the value Muser = Udao.selectdal (User) ' determines whether to query to record if IsNothing (muser.username) then Throw New Exception ("Query failed , please check your username and password! ") ' did not record Else MsgBox (" The query succeeded, congratulations! "," cue box ") ' has a record end If return Muser ' returns the entity end FunctionEnd class</span>
Layer D:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" > ' Referencing namespaces imports System.dataimports System.Data.SqlClientImports Loginmodel ' d layer, mainly on the original data (database or text files, such as the form of data storage) operation layer, need to refer to the physical layer ' public Class sqlserveruser public Conn as New SqlConnection (" Server=zy-pc;database=login;user Id=sa;password = 1 ") ' Create database connection public Function Selectdal (ByVal user as user) A S loginmodel.user ' transfer entity login for easy invocation of parameters in entities Dim reader as SqlDataReader ' defines variable reader of type SqlDataReader Dim Muser As New Loginmodel.user Dim sql As String = "Select Username,password from T_users where [Email&nbs P;protected] and [email protected] "database query statement Dim cmd as New SqlCommand (SQL, conn) ' Create SqlCommand Object Cmd. CommandText = SQL ' gets the concrete content of the SQL statement cmd. CommandType = CommandType.Text ' Gets the specific type of the above SQL statement, CMD. Parameters.Add (New SqlParameter ("@UserName", User.username)) cmd. Parameters.Add (New SqlParameter ("@PassWord", User.password)) Conn. Open () ' opens data connection reader = cmd. ExecuteReader () ' Executes the query statement and generates a DataReader ' read the query to the data and returns it to the corresponding property while reader. Read () Muser.username = reader. GetString ("UserName") ' assigns value to @username Muser.password = reader. GetString ("PassWord") ' assigns an End while return Muser ' to @password to return the entity conn to the query. Close () ' Closed connection End functionend class</span>
The "increase" of the four key weapons of three-tier database