Use tableadapter Configuration Wizard to create data access layer 2

Source: Internet
Author: User
Use tableadapter Configuration Wizard to create a data access layer

In Visual Studio 2005, tableadapter configuration is added.
To easily create a data access layer. First, we understand what tableadapter is. A tableadapter connects to the database, executes query statements or stored procedures, and fills the returned result set in the datatable. Tableadapter
The Configuration Wizard allows you to create and edit data sets in a typed dataset mode, which is very convenient.

First, use the C # language to create a web project named ntierexample, such:

To create a data access layer, right-click the project menu and choose "Add new item" from the shortcut menu ". In the pop-up "Add new
In the "item" dialog box, select the "dataset" type. Enter "authors. XSD" in the file name and click "add", as shown in:

When you click "add", the system will prompt whether to put the file in the app_code directory, because vs. net
2005, usually put the data access layer files in this folder for convenient management. Select "OK" and place the XSD file in the app_code folder. Next
"Tableadpater" sets the wizard window. First, we need to specify the database string to connect to, and save the connection string to the Web. config file, and select
"Next" to go to the next step.

In this step, select the command type. Because we have just created a stored procedure, select "use existing store procedure", for example:

Click "Next" to enter the next window and ask which stored procedure is used, for example:

Here, we choose to use the "getauthors" stored procedure, and then select "Next" to go to the next step, such:

Here, we need to specify which method to use the getauthors stored procedure to return the dataset. We choose "return
Datatable "is selected, and the getauthors method is specified to be returned as a datatable. Select "Next". If the following window appears
The data access layer is generated.

After you click the "finish" button, Visual Studio automatically generates some classes. After these classes are generated, we change the class to authors. In this way, the final output is shown in:

Next, follow the steps above. Similarly, use the "tableadapter" Wizard and select "data-add-tableadapter" in the tool menu bar.
Add a tableadapter. This time, the "gettitlesbyauthor" stored procedure is selected, and the returned method is
"Gettitlesbyauthor", other steps are the same as those for generating "getauthos". Finally, change the name of the generated class to "authortitles", as shown in
As shown in

Create logic layer

Next, we will create a logic layer. In this example, the logic layer is very simple and serves as a description. First, we create a class authrobiz class, put it in the app_code folder, andCodeModify as follows:

Public class authorsbiz
{
Public authorsbiz ()
{}

Public datatable getauthors ()
{
Authorstableadapters. authorstableadapter authordb = new
Authorstableadapters. authorstableadapter ();
Return authordb. getauthors ();
}
Public datatable getauthortitles (string authorid)
{
Authorstableadapters. authortitlestableadapter authordb = new
Authorstableadapters. authortitlestableadapter ();
Return authordb. gettitlesbyauthor (authorid );
}
}

From the code above, we can see that the "authors. XSD "typed dataset class. In the code, you can use the authorstableadapters class to call the class, where authordb is an instance of the authorstableadapters class.

Create presentation layer

In ASP. NET
2.0. when creating the presentation layer, you can use the master-page technology to easily build pages. Mater-page means that you can first build a page master
Framework template structure, and then place a contentplaceholder control in it, in which the content of other child pages will be displayed. In other subpages, you only need to reference
Master page, and then modify the content of the contentplaceholder control.

First, add a new "master" type file in the project, name it "commonmaster", and enter the following code:

<% @ Master language = "C #" %>
<HTML>
<Head id = "head1" runat = "server">
<Title> master page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Table id = "Header" style = "width: 100%; Height: 80px" cellspacing = "1"
Cellpadding = "1" border = "1">
<Tr>
<TD style = "text-align: center; width: 100%; Height: 74px;" bgcolor = "teal">
<Asp: Label runat = "server" id = "Header" font-size = "12pt" font-bold = "true">
Authors Information
</ASP: Label>
</TD>
</Tr>
</Table>
<B/>
<Table id = "leftnav" style = "width: pixel PX; Height: 100%" cellspacing = "1"
Cellpadding = "1" border = "1">
<Tr>
<TD style = "width: 100px">
<Table>
<Tr>
<TD>
<A href = "home. aspx"> Home </a>
</TD>
</Tr>
<Tr>
<TD>
<A href = "authors. aspx"> authors list </a>
</TD>
</Tr>
</Table>
</TD>
</Tr>
</Table>
<Table id = "mainbody" style = "Left: 120px; Vertical-align: Top; width: 848px;
Position: absolute; top: 94px; Height: 100% "border =" 1 ">
<Tr>
<TD width = "100%" style = "vertical-align: Top">
<Asp: contentplaceholder id = "middlecontent" runat = "server"> </ASP: contentplaceholder>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

Next, we will first create to display the authors Page's authors. the ASPX page, because the page framework must be maintained all the time, you can use the maser-page technology to introduce the created commonmaster page when creating a page, such:

Click the Add button, as shown in. Select the created commonmaster page, for example:

Enter the following code:

<% @ Page Language = "C #" masterpagefile = "~ /Commonmaster. Master "%>
<Asp: Content ID = "content1" contentplaceholderid = "middlecontent" runat = "server">
<Asp: objectdatasource runat = "server" id = "authorssource" typename = "authorsbiz"
Selectmethod = "getauthors">
</ASP: objectdatasource>
<Asp: gridview runat = "server" autogeneratecolumns = "false" id = "authorsview"
Performanceid = "authorssource">
<Alternatingrowstyle backcolor = "Silver"> </alternatingrowstyle>
<Columns>
<Asp: hyperlinkfield datatextfield = "au_id" headertext = "Author ID"
Datanavigateurlfields = "au_id"
Datanavigateurlformatstring = "authortitles. aspx? Authorid = {0} ">
</ASP: hyperlinkfield>
<Asp: boundfield headertext = "last name" datafield = "au_lname"> </ASP: boundfield>
<Asp: boundfield headertext = "first name" datafield = "au_fname"> </ASP: boundfield>
<Asp: boundfield headertext = "phone" datafield = "phone"> </ASP: boundfield>
<Asp: boundfield headertext = "Address" datafield = "Address"> </ASP: boundfield>
<Asp: boundfield headertext = "city" datafield = "city"> </ASP: boundfield>
<Asp: boundfield headertext = "state" datafield = "state"> </ASP: boundfield>
<Asp: boundfield headertext = "Zip" datafield = "Zip"> </ASP: boundfield>

</Columns>
</ASP: gridview>
</ASP: content>

Note: The objectdatasource control is used. With this control in. NET 2.0, the presentation layer and logic layer can be easily communicated. The Code is as follows:

<Asp: objectdatasource runat = "server" id = "authorssource" typename = "authorsbiz"
Selectmethod = "getauthors">
</ASP: objectdatasource>

The typename attribute is specified as the authorsbiz class of the logic layer we created earlier. To obtain data, the selectmethod method is used.
The previously created getauthors method. Of course, you can also use updatemethod, insertmethod, and deletemethod in other scenarios.
You can also add parameters, such as the authortitle. ASPX page to be created next. The Code is as follows:

<% @ Page Language = "C #" masterpagefile = "~ /Commonmaster. Master "%>
<Asp: Content ID = "content1" contentplaceholderid = "middlecontent" runat = "server">
<Asp: objectdatasource runat = "server" id = "authortitlessource" typename = "authorsbiz"
Selectmethod = "getauthortitles">
<Selectparameters>
<Asp: querystringparameter type = "string" direction = "input" name = "authorid"
Querystringfield = "authorid"/>
</Selectparameters>
</ASP: objectdatasource>
<Asp: gridview runat = "server" id = "authortitlesview" performanceid = "authortitlessource">
<Alternatingrowstyle backcolor = "Silver"> </alternatingrowstyle>
</ASP: gridview>
</ASP: content>

In the above Code, when you click an author name on the authors. ASPX page, all the works of the author are returned on the authortitle. ASPX page. Institute
In the objectdatasource control, the selectparameters parameter is used to specify the authorid parameter passed in for query. Finally
Bind the gridview to the objectdatasource control.

Finally, run our code and the result is shown in the following two figures:


Summary

In ASP. NET 2.0, we can use the powerful functions of SQL Server 2005 to use. net.
The language creates a stored procedure and uses the tableadapter Wizard to easily create a data access layer. Using the features of the objectdatasource control, you can easily communicate
Presentation layer and logic layer.

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.