Three-tier architecture instance for VB. NET

Source: Internet
Author: User
Tags connectionstrings

L3 instances

First of all, I found that the three parts are used for some object-oriented features, especially Object Instantiation. If you do not pay attention to it, you will be confused, just like me, slowly watching the flowers in the fog, and finally come out, but the use of events is quite.

Object instance, VB.net
And C # are good choices. Here I first use C # And then VB.net. Some of their syntaxes are somewhat different. For declaration, C # is directly stated in the previous statement,
Ervb. NET is declared later, and is the class name or type followed by later.

The reference of the namespace is not very large,

Using and core are named C #, and imports + core is named VB.net.

Of course, you must add references.

The arrays and configuration files used during this period are not described in detail here.

Briefly describe the configuration file.

The configuration files of the two are very small.

 

1. appsettings configuration section

 

<! -- Database connection string --> <? Xmlversion = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedruntimeversion = "v4.0" SKU = ". netframework, version = v4.5 "/> </startup> <etettings> <clear/> <add key =" connstring "value =" Server = 192. 168. **. * **; database = threelayerssystem; uid = sa; Password = 123456; "/> </appsettings> </configuration>

 

Note that the key and value correspond to each other. This is an instance of the remote reverse query database.

 

2 connectionstrings configuration section:
<?xmlversion="1.0"encoding="utf-8" ?><configuration>    <startup>         <supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5" />    </startup><connectionStrings>    <clear />       <add name="ConnectionStrings"connectionString="Data Source=192.168.**.***; DataBase=ThreeLayersSystem; User ID=sa; Password=123456"/>       </connectionStrings></configuration>

 

The difference is almost small.

 

The following describes how to create a specific instance.

First, create a blank solution.

 

 

Then create Layers

Add new projects in sequence

WINDOS form, business logic layer (Class Library), data access layer (Class Library), entity layer (Class Library)

Then add references between layers

 

Add business logic layer and real layer to presentation layer

 

Add a data access layer and an actual layer to the business logic

 

Add an Object layer to the data access layer.

 

Layer D also adds a reference for system. configuration to reference the configuration file.

 

The following shows the specific instance code, which is an example of VB. NET code.

 

The UI code for the presentation layer is as follows:

 

The centralized configuration file is on the u layer

WINDOS
Form code

Import reference

 

Imports cbllimports modelpublic class form1privatesub cmdok_click (sender asobject, e aseventargs) handles cmdok. click me. login (txtuid. text, txtpwd. text) endsub ''to pass the parameter and accept the returned parameter private usermanager asnew BLL. bll publicsub login (byval userid asstring, byval userpwd asstring) dim equal asstatus = usermanager. usermanger (userid, userpwd) if (equal = status. success) then dispose (userid) me. close () elseif (equal = status. fail) Then msgbox ("incorrect user name or password") endifendsubend class

Business logic layer code

 

Imports dalimports system. data. sqlclientimports modelnamespace cbll public class bll a method has several return values and several parameters, depending on what is called at the interface layer, that is to say, you need to create a function that returns the enumerated type to judge the value passed in the presentation layer, and check whether the verification is successful or not. Public Function usermanger (userid as string, userpwd as string) as Boolean dim userdal as new Dal. dal dim userinfo as new user userinfo = userdal. query (userid, userpwd) 'determines that the password is true like the database, otherwise it is fail if (userinfo. _ userid <> userid or userinfo. _ userpwd <> userpwd) then return false else return true end if end function end classend namespace

 

Data access layer
Imports modelimports system. data. sqlclientimports system. configurationimports systempublic class Dal 'defines the private variable private userinfo asnewuser private RS as sqlclient. sqldatareader private conn as sqlclient. sqlconnection private com as sqlclient. sqlcommand private strconnection asstring 'executes the connection string. The configuration file is used here. Publicsubnew () Conn = new sqlclient. sqlconnection strconnection = system. configuration. configurationmanager. appsettings ("connstring") Conn. connectionstring = strconnection try Conn. open () console. writeline ("data test succeeded") catch ex asexception throw ex finally 'conn. close () endtry endsub publicfunction query (byval userid asstring, byval userpwd asstring) asuser' queries user information and returns user information COM = new sqlclient. sqlcommand ("select * From t_user where userid = '" + userid + "' and userpwd = '" + userpwd + "'", Conn) Try rs = com. executereader () while (RS. read) userinfo. _ userid = Rs. item ("userid") userinfo. _ userpwd = Rs. item ("userpwd") endwhile return userinfo catch ex asexception throw ex finally if not isnothing (conn) then' close () endif endtry endfunction endclass

 

The entity layer corresponds to a database field, the user class

 

 

Imports system. dataimports system. data. sqlclientimports system. configurationimports model. statuspublicclassuser # region "defines the attribute variables in the User table" Private userid asstring ''user ID private userpwd asstring'' User Password private username asstring ''user name # endregion # region" Get database variables" publicproperty _ userid asstring get return userid endget set (value asstring) userid = value endset endproperty publicproperty userstate asstatus get return state endget set (value asstatus) State = value endset endproperty publicproperty _ userpwd asstring get return userpwd endget set (value asstring) userpwd = value endset endproperty # endregionendclass

 

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.