Vb. NET three-tier architecture instance-Login

Source: Internet
Author: User

The great benefits of encapsulation to programming have been felt, and the idea of object-oriented programming has been deeply rooted. With the guidance of object-oriented thinking, the software has been quickly

's development. With this development trend, the three-tier architecture has emerged.

from the physical structure, the layer three can be a client, an application server, a database server. Logically it is the presentation layer (Presentation layer), the business logic

Layer (Business Logic layer), data access layer Access layer). The distinction level embodies the idea of "high cohesion and low coupling" . In the software architecture set-up

Medium, layered structure is one of the most common and most important structures. The following is a small instance of login written by vb.net to understand the beauty of layer three.

I. Building the Environment

1. Basic Principles

UI layer: Responsible for capturing user data and displaying data

BLL layer: Responsible for receiving the data from the UI layer, processing, the data to the DAL layer processing, after processing is completed, return data to the UI

DAL layer: Access to the database that allows the BLL layer to access and return data

2, three layer frame

3, build the database, containing user information of the table

Second, the code implementation

1. Entity

Role: Encapsulates user data, allowing data to be transferred in layer three

Public Class UserInfo    ' username attribute    Private _username As String Public property    username as String        Get            Return _username        End Get        Set (value as String)            _username = value        End Set    End    property ' pwd attribute    Private _pwd as String Public property    PWD As String        Get            Return _pwd        End Get        Set (value as String)            _pwd = Value        end Set    end Propertyend Class

2. Presentation Layer UI

Public Class Form1    Private Sub btnlogin_click (sender as Object, e as EventArgs) Handles btnlogin.click        Try            ' received The data for the presentation layer            Dim euser2 as new Entity.userinfo         ' instantiates the new UserInfo, which is used to pass the B-layer entity            euser2. UserName = TxtUserName.Text.Trim  ' assigns a value euser2 to the UserName attribute of the Euser2 object            . PWD = TxtPassword.Text.Trim       ' to Euser2 object's PWD attribute assignment            ' call B-layer            Dim Mgr as New loginbll.loginmanager   ' Instantiate a B-Layer Loginmanager object Mgr            Dim euser3           as Entity.userinfo ' defines a parameter of type UserInfo, receives the parameter returned by layer b            euser3 = Mgr. Userlogin (euser2)         ' through Mgr Object, with B-layer interaction        Catch ex as Exception                   ' exception handling            MessageBox.Show (ex. Message.tostring ())        End Try    End subend Class

3. Business Logic Layer BLL

Public Class Loginmanager Public    Function userlogin (ByVal User as Entity.userinfo) as Entity.userinfo   ' The variable that receives the Entity.userinfo type        Dim Udao As New Logindal.userdao   ' instantiates an object Udao for accessing the D-Layer        Dim eUser1 As Entity.userinfo      ' Define an entity parameter to receive a D-layer parameter, return all attributes within the U entity        eUser1 = udao.selectuser (user)   ' to the D-Layer incoming entity type parameter User, and get a return value with entity parameters        ' Determine if the record is queried, if so, log in successfully and return entity Euser1        If isnothing (euser1.username)            then Throw New Exception ("Login failed, check user name and password")        Else            MsgBox ("Login Successful")            
  Return eUser1        End If    End FunctionEnd Class

4. Data Access Layer DAL

' Reference space name ' imports System.Data.SqlClientPublic Class Userdao ' CREATE DATABASE connection public conn as New SqlConnection ("server=wangju-pc ;d atabase=login;user id=sa;password=123456 ") ' Returns an entity UserInfo function Public Function selectuser (ByVal user as Entity.useri NFO) as Entity.userinfo ' pass entity UserInfo, instead of parameter id,username, so it is convenient to call the parameters in the entity Dim reader as SqlDataReader ' definition type is SqlDataReader Variable Reader Dim euser As New Entity.userinfo ' instantiates a new UserInfo Dim sql As String = "Select Userna Me,password from Users where [email protected] and [email protected] "Dim cmd as New SqlCommand (SQL, Conn The ' SqlCommand method is a constructor, with arguments for an executed SQL statement, and one for the connection object cmd. CommandText = SQL ' gets the T-SQL statement cmd to execute. CommandType = CommandType.Text ' Gets a value that indicates how to interpret the CommandText property of CMD. Parameters.Add (New SqlParameter ("@UserName", User.username)) ' entity's UserName parameter cmd. Parameters.Add (New SqlParameter ("@PWD", USER.PWD)) Conn. Open () ' Opening database connection ' ExecuteReader () return type is SqlDataReader, whichA method is used for the user to query the operation reader = cmd. ExecuteReader () ' executes the SQL statement while (reader. Read ()) ' Use the SqlDataReader object Read () method for progressive reading, and when the field is non-empty, execute the following statement, Euser.username = reader. GetString (0) ' Take out the attribute value in column No. 0 euser.pwd = reader. GetString (1) ' Take out the attribute value in column 1th and return Euser ' returns an object conn. Close () End functionend Class

Third, Login effect

User name: Wangju

Password: WJ

Iv. Summary of Learning

By implementing a three-tier login small instance, you can see how the interaction between the three tiers is implemented, and the methods used in each layer. The first study, simply swallowed Geoscience,

Back again to learn, with more questions and puzzles. In the process of solving problems, but also very tangled. With the solution of a problem, the idea slowly clear, the heart is comfortable

more, at this stage of learning encountered some interesting functions, will be written in the next blog.  

Vb. NET three-tier architecture instance-Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.