VB. NET + layer-3 Login

Source: Internet
Author: User

Three layers have been learned for some time. I began to think that I could use C # To write a program, and I wrote a program for login in C #, which was actually used in the data center, I still felt very hard, so I decided to use vb.net. The following is my login using vb.net. You can make some reference.

This is the three-tier architecture

Database Table T_User_DAL: UserI, Level, Password, Head, computer

First, let's take a look.

Entity layer:

Public Class UserInfoEntity # Region "definition variable" Private _ userID As String Private _ level As String Private _ Head As String Private _ password As String # End Region Public Property UserID () as String 'transfers every object in the T_User_DAL table. The object is created based on the table, not the Get Return _ userID End Get Set (value As String) function) _ userID = value End Set End Property Public Property Level () As String Get Return _ level End Get Set (ByVal value As String) _ level = value End Set End Property Public Property Head () As String Get Return _ Head End Get Set (value As String) _ Head = value End Set End Property Public Property Password () as String Get Return _ password End Get Set (value As String) _ password = value End Set End PropertyEnd Class


DAL layer:

To reduce coupling, you must extract the connection string and do not write one side in each DAL.

Imports System. dataImports System. data. sqlClientPublic Class SqlUtil Public Shared Function connstring () As String 'connstring = "Server = 192.168.24.183; Database = ReconsitutionCharge_sys; User ID = sa; password = 123456 "'connstring =" Server = mx; Database = ReconsitutionCharge_sys; User ID = sa; Password = 123456 "' connection string End FunctionEnd Class






Code for the DAL layer of the T_User_dal table:

Imports System. data. sqlClientImports System. dataPublic Class UserDAL Public Function SelectUser (ByVal user As Entity. userInfoEntity) As Entity. userInfoEntity Dim conn As New SqlConnection 'define connection open database Dim cmd As New SqlCommand' define DATABASE Command conn = New SqlConnection (SqlUtil. connstring () 'instantiate the string cmd returned by SQLUntil. connection = conn cmd. commandText = "Select * From T_User_DAL Where UserID = @ UserID and Password = @ Password "'Stored Procedure cmd. parameters. add (New SqlParameter ("@ UserID", user. userID) 'value assignment, passing the cmd parameter. parameters. add (New SqlParameter ("@ Password", user. password) cmd. commandType = CommandType. set the 'text' CommandText attribute to the name of the stored procedure. When the Execute method is called, the command will Execute this stored procedure conn. open () 'Open the connection Dim reader As SqlClient. sqlDataReader 'reads the table reader in the database = cmd. executeReader () Dim users As New Entity. userInfoEntity 'instantiate the object While (reader. read () 'if users Is Nothing then' user = New Entity. userInfoEntity 'end If users. userID = reader. getString (reader. getOrdinal ("UserID") users. password = reader. getString (reader. getOrdinal ("Password") End While conn. close () Return users End FunctionEnd Class



BLL layer:

Public Class LoginBLL Function UserLogin (ByVal user As Entity. userInfoEntity) As Entity. userInfoEntity Dim uDAL As New DAL. userDAL 'instantiate UserDAL Dim users As New Entity in DAL. userInfoEntity 'instantiates the Entity layer information table users = uDAL. selectUser (user) 'selectuser returns an Entity Class and assigns the user Return users' value to it to Return the users Entity End FunctionEnd Class.



UI Layer:

Interface Design:




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + cda-vcd4kphbyzsbjbgfzcz0 = "brush: java;"> Private Sub btnLogin_Click (sender As Object, e As EventArgs) Handles btnLogin. click Dim mgr As New BLL. loginBLL 'instance business layer Dim user As New Entity. userInfoEntity 'instance Entity Dim users As Entity. userInfoEntity 'receives the object, which is used to receive the data type returned by UserLogin (). If there is no New, the user is not instantiated. userID = txtUserID. text. trim () 'is used to input the value user to the Object layer. password = TxtPassword. text. trim () If txtUserID. text = "" Then 'Determine whether the input is correct MsgBox ("the user name cannot be blank! ") Return End If TxtPassword. Text =" "Then MsgBox (" the password cannot be blank! ") Return End If Try 'users Receiving Entity users = mgr. userLogin (user) If (users. userID Is Nothing And users. password Is Nothing) then' if users Is not empty, the logon Is successful! MsgBox ("Login Failed, username and password do not match") Return Else MsgBox ("Login successful! Login user: "+ user. UserID) End If Catch ex As Exception MsgBox (ex. Message () End Try End SubEnd Class

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.