Three-tier logon instance for VB. NET

Source: Internet
Author: User

The three-tier instance of VB. NET does not find the source code from the Internet. Basically, it is C. I have just learned from the big talk design model and have been mixed up by its good multi-language rules. Fortunately, there is still a little bit of VB basics, after debugging, discussion, and tangle for many days, this is now the most simple login instance. If you are using the three-tier idea for the first time, you are welcome to make an axe!

First, we will briefly introduce the next three layers: **************************************** **************************************** **************************************** **************************************** ************************

An image is sufficient to describe:

We can see that the division of labor between layers U, B, and D is clear and interrelated, leading to a loose coupling relationship.

Although it is a layer-3 architecture, there is actually an entity layer. Strictly speaking, entity cannot be regarded as a layer. It is dispensable in a layer-3 architecture. It is actually the most basic thing in Object-Oriented Programming: class. A table is a class, a news is also a class, Int, String, doublie is also a class, it is just a class.
In this way, the location of the model in the three-tier architecture is the same as that of the int, string, and other variables. It has no other purpose and is only used for data storage, it only stores complex data. Therefore, if the objects in your project are very simple, you can build a three-tier architecture by directly passing multiple parameters without using the model.
So why do we need a model? What are its advantages. For example, when passing parameters between layers, you can:


You can also do this:


Which of the two methods is better. Obviously, it must be much better than the second one.

Let's take a look at the logon effect: **************************************** **************************************** **************************************** **************************************** ************************

Based on the idea of serving the people wholeheartedly, set the text box to get the tabindex attribute, the access button Utton is set to "login" by default, and the cancelbutton is set to "cancel" by default, it will be very convenient to operate in the future.

If both the password and the user are correct, the prompt is as follows:

If the password or user is incorrect, the prompt is as follows:

Then the database design: **************************************** **************************************** **************************************** **************************************** ************************

Create a database named login in SQL Server 2008 and create an users table. The design is as follows:

Add wlc and 1 under the username and PWD fields respectively.

The Code is as follows: **************************************** **************************************** **************************************** **************************************** ************************

U layer:

Public class loginui private sub btnlogin_click (sender as object, e as eventargs) handles btnlogin. click try to obtain the data dim euser2 as new entity of the presentation layer. userinfo' refers to the new userinfo, which is used to pass the entity dim euser3 as entity at Layer B. userinfo' defines a parameter of the userinfo type, which is used to assign values to euser2.username = txtusername. text. trim' passes the user name to the username euser2.pwd = txtpassword in the Entity layer. text. trim' passes the password to pwd' on the physical layer to call layer B. log on to determine whether dim Mgr as new BLL. loginmanager euser3 = Mgr. userlogin (euser2) catch ex as exception MessageBox. show (ex. message. tostring () end try end sub private sub btnclose_click (sender as object, e as eventargs) handles btnclose. click me. close () end sub

Layer B:

Public class loginmanager public function userlogin (byval user as entity. userinfo) as entity. userinfo dim udao as new Dal. userdao 'instantiate the userdao object dim euser1 as entity in layer D. userinfo' defines a parameter of the Real-layer userinfo type, which is used to assign values to euser1 = udao. selectuser (User) 'determines whether a record has been queried. If yes, the logon is successful and the entity euser1 if isnothing (euser1.username) Then throw new exception ("Logon failed, check the user name and password! ") Else msgbox (" Login successful, go to system now... "," congratulations ") return euser1 end if end functionend class

Layer D:

'Reference the space name imports system. dataimports system. data. sqlclientimports entity 'd layer, only involves data operations, need to reference entity layer entitypublic class userdao 'create database connection public conn as new sqlconnection ("Server = localhost; database = login; user ID = sa; Password = 123456 ") Public Function selectuser (byval user as userinfo) as entity. userinfo' transmits the object userinfo, instead of the parameter ID and username. This allows you to easily call the dim reader as sqldatareader variable for defining the type sqldatareader. Euser as new entity. userinfo 'new userinfo' is equivalent to the following program: dim SQL as string = "select ID, username, PWD from users where username = '" user. username "'and Pwd ='" user. PWD "'" And write this block to prevent SQL injection, that is, security considerations. '@ Username is equivalent to passing a parameter. ("@ username", user. username) is equivalent to passing a parameter to the parameter name. Dim SQL as string = "select username, PWD from users where username = @ username and Pwd = @ PWD" dim cmd as new sqlcommand (SQL, Conn) 'create the sqlcommand object cmd. commandtext = SQL 'get the specific content of the SQL statement cmd. commandtype = commandtype. 'text' gets the specific type of the preceding SQL statement, which is select cmd. parameters. add (New sqlparameter ("@ username", user. username) 'if used as an euser. if username is used, the following error occurs: cmd. parameters. add (New sqlparameter ("@ PWD", user. PWD) Conn. open () 'Open the data connection reader = cmd. executereader () 'executes the query statement and generates a datareader 'to read the queried data and return it to the corresponding attribute while reader. read () 'obtain the data of the corresponding fields in the database' array must be read from scratch, otherwise it will exceed the limit of euser. username = reader. getstring (0) euser. pwd = reader. getstring (1) end while return euser 'returns the queried object Conn. close () 'close connection end functionend class

Entity layer ):

'Entity layer, stores various attributes of a user. 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 property private _ PWD as string public property PWD as string get return _ PWD end get set (value as string) _ Pwd = value end set end property

The demo has been uploaded to the csdn Resource (layer-3 login instance (VB. NET) to notify readers ~

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: 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.