Go: ASP. NET Advanced Applications (2)

Source: Internet
Author: User
Tags eval
Asp.net| advanced three-layer structure and its application

Concept and environment

Asp. NET three layer result development method, actually its idea is same as Java. The three-tier architecture in Java is the front-end HTML, JSP, Servlet, the middle layer is JavaBean, EJB, followed by the database server. In the ASP.net, the front section is HTML, ASP, ASPX, etc., the middle layer is a. dll control compiled from files such as. vb,. cs, and then the database server.

In our three-tier architecture, our database layer connects and operates through the middle tier, passing parameters to the middle layer and accepting the middle-tier parameters. In our asp.net, our primary focus is on the data interaction between our middle tier and the front-end.

We generally collectively referred to as the middle tier components, components can be compiled with the. vb, can also be compiled with the. cs file. The middle tier is typically a. dll file. Microsoft's. NET technology is simpler in this respect than any previous version of it, and that's one of its benefits. We used to register a. dll file, there is a registration is restarted, and on. NET, our. dll file is used, we do not have to consider the issue of registration.

Before you have 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 namespace, the file name to compile, and the corresponding command name, and then run it. It sounds complicated, which is what many beginners are afraid of when compiling the first. dll file. But it's easy to do. Let us give an example to illustrate the writing of the. bat file, assuming we have a file named: Saidy.vb, we want to compile it into a saidy.dll file, which uses System, System.Data, System.Data.SQL namespace, we can create a Dblink.bat file that reads 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 compiled. vb program command, if it is compiled. cs file, then the command will be different, we assume that there is a Saidy.cs file, according to the above requirements, we compile the following:

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 Microsoft's Vs.net programming environment, we need not so much trouble, we can compile VB or VC program as easy to compile the. dll file. Microsoft's Vs.net is a synthesizer, the integration of various languages, in this environment can write different languages of the program. The specific application we will introduce in the special chapters.

An example based on a three-tier architecture

We use concrete examples to illustrate the application of the three-tier architecture, and we build a small project to illustrate the problem. Sometimes for security purposes, we usually encapsulate the connection to the database with a dynamic connection library file, so that we need to compile the. vb or. cs file that connects the write database to the dynamic Connection library. dll file. Even we compile the related action pages for the database into. dll files.

Here is the main part of our database connection and operation file Dblink.vb, the connection to the database:

Dim Dbl as SqlConnection

To the operation of the database, we write it in a method that returns the corresponding value:
Function GetData () as DataView
Dim Scomm as Sqldatasetcommand
Dim SDS as DataSet
Dim SStr as String
Dbl = New SqlConnection ("server=localhost;uid=sa;password=;d atabase=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 above connection variable with the database, the function of which is to select all the elements from the table "color" and return the form of the table structure. The complete code is as follows:


Imports System
Imports System.Data
Imports System.Data.SQL
' Create a name space
Namespace DB
' Create a class
Public Class Dblink
' Establish a connection to the database
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=;d atabase=howff")
Dim SStr as String
SSTR = "SELECT * FROM Color"
Scomm = New Sqldatasetcommand (sStr, Dbl)
' Populate data
SDS = New DataSet ()
Scomm.filldataset (SDS, "color")
' Return
return sds.tables ("Color"). DefaultView
End Function
End Class
End Namespace
We're going to write a front end. Page saidy.aspx, we first want to introduce the namespaces we create:

<%@ Import namespace= "DB"%>

When the page is loaded, we use this method:
Sub Page_Load (Sender as Object, E as EventArgs)
' To create a new object
Dim newdb as Dblink
newdb = new Dblink ()
' Data sources
Products.datasource = Newdb.getdata ()
' Data binding
Products.databind ()
End Sub
Here's a look at our complete code (advanceapp\dblink.aspx):
<%@ Import namespace= "DB"%>
<script language= "VB" runat= "Server"
Sub Page_Load (Sender as Object, E as EventArgs)
' To create a new object
Dim newdb as Dblink
newdb = new Dblink ()
' Data sources
Products.datasource = Newdb.getdata ()
' Data binding
Products.databind ()
End Sub
</script>
<body style= "font:10pt Verdana" bgcolor= "CCCCFF"
<BR> <BR> <BR>
<CENTER>
</CENTER>
<BR> <BR>
<CENTER>
<asp:datalist id= "Products" Showheader=false showfooter=false repeatcolumns= "2" repeatdirection= "Horizontal" borderwidth=0 runat= "Server" >
<template name= "ItemTemplate"
<table>
<tr>
<TD width= "style=" text-align:center; font-size:8pt; Vertical-align:top;
Height:50 ">
<p>
<%# DataBinder.Eval (Container.DataItem, "id")%> <br>
<%# DataBinder.Eval (Container.DataItem, "name", "{0:c}"). ToString ()%>
</td>
</tr>
</table>
</template>
</ASP:DataList>
</CENTER>
</body>

We see that in this page, there are no statements that interact with the database, so that we have a good encapsulation of the data operation.


Related Article

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.