VB. NET journey-login to a three-tier architecture
Initial contact Layer 3
- Layer 3 refers to the display layer, business logic layer, and data access layer, which are used for "High Cohesion and low coupling" services.
- In addition to the above three layers, an entity layer must be included in a program. In my understanding, the entity layer is opposite to the table in the database, the attribute of an object is relative to the field in the data table. You can also add the appearance layer, data interface, and abstract factory layer as needed. This should be added according to the program's needs. Just like the login we are going to implement today, we don't need the appearance, the data interface, and the abstract factory. In fact, even the business logic layer can be omitted, because there is no logical operation here.
- The display layer references the business logic layer. The business logic layer references the data access layer. The Layer 3 can reference the entity layer as needed.
- In this example, a small mistake was made, that is
- It took me one night and half an hour to solve this problem. Just as I was about to bite my tongue, I suddenly noticed:
-
- Q: What is the relationship between the four projects in solution manager? The answer is: they are four different assemblies. They belong to the solution, but they can also be added by other solutions by adding existing projects. (In VB. NET, whether it is a solution, assembly, class, form, it is called a project before being added)
- Right-click the existing project added by the solution and select assembly.
- By right-clicking the assembly, you can add existing projects as classes or forms.
- Click the file in the menu bar->-a new project or an open project is a solution.
- Do you know what I made? As soon as you don't see it, my mistake is that I changed the names of the above four sets to LogIn, so the above error occurs.
- Next, let's take a look at my code. You must understand the three words "First Contact" that I wrote at the beginning. It's already good to write such code.
Display Layer <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kphbyzsbjbgfzcz0 = "brush: java;"> Imports LogIn. BLLImports LogIn. modelPublic Class frmLogin Public Sub btnLogIn_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles btnLogIn. click Dim admininfo As AdminInfoEntity Dim manager As New adminBLL Dim _ name As String Dim _ password As String _ name = txtAdminName. text _ password = txtAdminPwd. text admininfo = manager. selectAdmin_info (_ name, _ password) 'calls the logic layer. The returned admininfo may be empty or not. If admininfo Is Nothing Then MessageBox. show ("Login Failed") Else MessageBox. show ("Login User" + admininfo. name) End If End Sub Private Sub btnCancel_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles btnCancel. click Me. close () End SubEnd Class Business logic layer
Imports LogIn. modelImports LogIn. DALPublic Class adminBLL Dim AdminInfo As AdminInfoEntity Dim AdminDAL As New AdminDAL 'verify that user logon information is correct Public Function selectAdmin_info (ByVal name As String, ByVal password As String) As AdminInfoEntity AdminInfo = AdminDAL. getAdminInfo (name, password) 'calls the data access layer. The returned admininfo may be null or not empty. Return AdminInfo End FunctionEnd Class
Data access layer
Imports System. data. sqlClientImports System. dataImports System. data. oleDbImports SystemPublic Class ConnectionToDB Public Function ConnectionToDB () As SqlConnection Dim con As New SqlConnection ("Data Source = Liu Ying-PC; Initial Catalog = Room_Charge_System; User ID = sa; password = 123456 ") 'connect to the database con. open () Return con End FunctionEnd Class
Imports SystemImports System. dataImports System. data. oleDbImports System. data. sqlClientImports LogIn. modelPublic Class AdminDAL Dim AdminInfo As AdminInfoEntity Dim con As New ConnectionToDB 'get user information Public Function getAdminInfo (ByVal name As String, ByVal password As String) as AdminInfoEntity the record Dim cmd As New SqlCommand ("select * from T_Admin_info where Name =" & "'" & name & "'" & "and Password = "&"' "& password &"'", con. connectionToDB () Dim reader As SqlDataReader reader = cmd. executeReader () While (reader. read () 'If the table contains records with the username and password corresponding to the text box, use the new keyword to call the AdminInfoEntity constructor so that admininfo is not empty, assign AdminInfo = New AdminInfoEntity AdminInfo to the attributes. ID = reader. getString (0) AdminInfo. name = reader. getString (1) AdminInfo. level = reader. getString (2) AdminInfo. password = reader. getString (3) End While Return AdminInfo End FunctionEnd Class
Entity Layer
'The user class provides the public attributes that can be called by other classes, and also the fields that can be called by members in the class. Other classes can use attributes to restrict unconditional operations on fields, for example, read-only, write-only, and range assignment. Public Class AdminInfoEntity Dim _ name As String Public Property Name () As String Get Return _ name End Get Set (ByVal value As String) _ name = value End Set End Property Dim _ id As String Public Property ID () As String Get Return _ id End Get Set (ByVal value As String) _ id = value End Set End Property Dim _ level As String Public Property Level () As String Get Return _ level End Get Set (ByVal value As String) _ level = value End Set End Property Dim _ password As String Public Property Password () As String Get Return _ password End Get Set (ByVal value As String) _ password = value End Set End PropertyEnd Class