First, create the corresponding class library and form in vs. The operations at each layer are as follows:
First, it is clear that the layer-3 instance I wrote is based on the entity in the real layer. The UI Layer references The bll layer and entity layer, The bll layer references the Dal layer and entity layer, and the Dal layer references the entity layer.
The entity class name in the database created by the entity layer (entity) is entityuserinfo.
Create a frmlogin form at the UI Layer,
The bll layer creates a class library named loginbll and the Dal layer creates a class library named logindal.
The code for each layer is as follows:
Entity: (mainly used for object transfer. It runs through layer B and layer D of the U layer)
Public class entityuserinfo defines two private attributes: Private ID as string private password as string '. This defines the attribute process and allows its classes to access this attribute public property userid () as string get return ID end get set (value as string) id = value end set end property public property PWD () as string get return password end get set (value as string) password = value end set end propertyend class
UI Layer: (user interface, receiving and real data)
Public class frmlogin private sub btnok_click (sender as object, e as eventargs) handles btnok. click 'instantiate the object userinfo dim userinfo as new entity. entityuserinfo 'instantiate the object loginbll dim loginbll as new BLL. loginbll assigns a string in the text box to the entity object userinfo so that the object can carry the parameter userinfo. userid = txtusername. text. tostring userinfo. pwd = txtpassword. text. tostring' instantiate a model object dim model as new entity at the Entity layer. entityuserinfo 'calls the BLL layer method (the return value is an object, and all object models defined above)' returns an object by calling the BLL layer method, compare whether the attributes of the model are consistent with those of the text box. Model = loginbll. userlogin (userinfo)
If Model. userid = txtusername. text and model. pwd = txtpassword. text then me. hide () applycation. show () else MessageBox. show ("Logon Failed") end if end sub
Bll layer: (the business logic layer calls the Dal layer method and is called by the UI Layer to connect the UI Layer and the Dal layer)
Imports entity. entityuserinfoimports Dal. logindalpublic class loginbll 'defines a function method. The parameter passed by userlogin is a model variable of the Object layer type. The returned value is a public function userlogin (byval model as entity. entityuserinfo) as entity. entityuserinfo 'defines the Dal layer object logindal dim logindal as new Dal. logindal 'defines the object userinfo dim userinfo as new entity. entityuserinfo 'calls the Dal layer method and returns the entity userinfo = logindal. selectuser (model) return userinfo end functionend class
Dal layer (data access layer for database connection)
Imports system. data. sqlclientimports system. dataimports entity. entityuserinfopublic class logindal 'database connection. database connection is defined as a constructor. When the logindal instance is instantiated, the database connection dim STR as string = "Server = .; database = charge_system; Integrated Security = sspi "'Here, the string uses the Windows authentication method, and all operations without the user name and password are required. Dim conn as new sqlconnection (STR) 'queries data in the database and runs the command statement public function selectuser (byval model as entity. entityuserinfo) as entity. entityuserinfo 'connect to database Conn. open () dim SQL as string = "select * From t_users where userid = '" & model. userid & "'and Pwd ='" & model. PWD & "'" dim cmd as new sqlcommand (SQL, Conn)' defines a sqldatareader type variable reader dim reader as sqldatareader reader = cmd. executereader () 'Because the ececutereader attribute of the CMD object returns a datareader, only one datareader type variable can be defined to receive data. 'Instantiate the object userinfo dim userinfo as new entity at the Entity layer. entityuserinfo 'reader. the returned value of read is true. Execute the value assignment operation for the attributes of userinfo while (reader. read () userinfo. userid = reader. getstring (0) userinfo. pwd = reader. getstring (1) end while 'returns the object userinfo return userinfo end functionend class
This is the end of a layer-3 instance. If you have any questions, please confirm.