ASP.NET Advanced Applications (2)

zhaozj2021-02-17  43

Three-layer structure and its application

Concept and environment

The three-layer results development method in ASP.NET, in fact, their thoughts are the same as Java. The three-layer architecture in Java is the front-end HTML, JSP, Servlet, the intermediate layer is JavaBean, EJB, and later is a database server. In ASP.NET, the front section is HTML, ASP, ASPX, etc., the intermediate layer is a .dll control, which is compiled. DLL control, and later is a database server.

In our three-layer architecture, our database layer is connected to the intermediate layer, and the front end passes the parameters to the intermediate layer, and accepts the parameters of the intermediate layer. In our ASP.NET, we focus on our intermediate layer and front-end data interaction.

We generally collect the intermediate layer as a component, and the components can be compiled with .vb, can also be compiled with .cs files. The intermediate layer is generally a .dll file. Microsoft's .NET technology is coming from this aspect than his previous version, which is also one of its hits. We have to register a .dll file, there is a registration is a restart, and on .NET, our .dll file is used, no need to consider registration questions.

Before there is no Visual Stutio.net, we use the .bat file to compile the .vb and .cs files into a .dll file, in the .bat file, we write the compiled file name, the associated name space, Compiled file name and the corresponding command name, then run. It sounds very complicated, which is also what many beginners are fear when compiling the first .dll file. But it is easy to do. Let's take an example to illustrate the writing of the .bat file, assume that we have a file name: saidy.vb file, we have to compile it into Saidy.dll files, which use system, system.data, system. Data.sql namespace, we can create a dblink.bat file, the content is as follows:

VBC /OUT:../bin/saidy.dll / t: library /r :system.dll /r:system.data.dll / r: system.data.sql.dll

DBLINK.VB

This is a command to compile the .vb program, if it is compiled .cs file, the command will be different. We assume that there is a Saidy.cs file, according to the above requirements, we compile:

CS /out:../bin/saidy.dll / t: library /r:system.dll /r:system.data.dll / r: system.data.sql.dll

DBLINK.CS

We can see that most of them are the same.

Of course, if we have a Microsoft's VS.NET programming environment, we don't have to comply with such a compilation .dll file like compiling VB or VC programs. Microsoft's VS.NET is a set of companies that integrate various languages, and can write out different languages ​​in this environment. Specific applications We will introduce them in specialized chapters.

An example of a three-layer architecture

Through the specific example, we explain the application of the three-story architecture, we build a small project to illustrate this problem. Sometimes for security, we usually encapsulate the connection with the database with a dynamic connection library file so that we should compile the write database .vb or .cs file into a dynamic connection library .dll file. Even we compile the relevant action page of the database into a .dll file.

Below is our main part of our database connection and operational file DBLINK.VB, the connection to the database:

DIM DBL AS SQLCONNECTION

For the operation of the database, we write it in a method and return the corresponding value:

Function getData () AS DataViewdim Scomm as SqlDataSetCommand

DIM SDS AS Dataset

DIM SSTR AS STRING

DBL = New SqlConnection ("Server = localhost; uid = sa; password =; database = howff")

SSTR = "SELECT * from color"

Scomm = New SqldatasetCommand (SSTR, DBL)

SDS = New DataSet ()

Scomm.FillDataSet (SDS, "Color")

Return SDS.Table ["color"]. DefaultView

END FUNCTION

Our sixth statement uses the connection variable of the database above. The function of our function is to select all the elements from the table "Color" and return to the form of the table structure. The complete code is as follows:

Imports system

Imports system.data

Imports system.data.sql

'Creating a namespace

Namespace DB

'Creating a class

Public Class Dblink

'Establish a database connection

DIM DBL AS SQLCONNECTION

'method

Public function getData () AS dataView

DIM Scomm as SqldatasetCommand

DIM SDS AS Dataset

DBL = New SqlConnection ("Server = localhost; uid = sa; password =; database = howff")

DIM SSTR AS STRING

SSTR = "SELECT * from color"

Scomm = New SqldatasetCommand (SSTR, DBL)

'Data input

SDS = New DataSet ()

Scomm.FillDataSet (SDS, "Color")

'return

Return SDS.Tables ("Color"). DefaultView

END FUNCTION

END CLASS

End Namespace

Let's write a front side to use the page Saidy.aspx, we must first introduce the namespace we created:

<% @ Import namespace = "db"%>

When loading, we use this method:

SUB Page_Load (Sender As Object, E as Eventargs)

'Creating a new object

Dim NewDB as dblink

Newdb = new dblink ()

'Data Sources

Products.DataSource = newdb.getdata ()

'Data binding

Products.DATABIND ()

End Sub

Let's take a look at our complete code (AdvanceApp / dblink.aspx):

<% @ Import namespace = "db"%>