ASP. NET 2.0 data Tutorial: how to add parameterization to the data access layer

Source: Internet
Author: User

Step 3: add parameterization method to the data access layer

So far, ProductsTableAdapter has only one method, GetProducts (), which returns all products in the database. Operations on all products are useful, but we often want to obtain information about a specific product or all products under a specific category. To add such a feature to our data access layer, we can add a parameterized method to TableAdapter.

Add parameterization: let's add a GetProductsByCategoryID (categoryID) method. To Add a new method to the DAL, let's go back to the DataSet Designer, right-click the ProductsTableAdapter, and select "Add Query )".

 

Figure 14: Right-click TableAdapter and select "add query"

The Wizard will first ask if we want to use an ad-hoc SQL statement to generate a new stored procedure or use an existing stored procedure to access the database. Let's choose to use SQL statements. Then, the wizard will ask us what type of SQL query we use. Because we want to return all products belonging to the specified category, we need to write a SELECT statement that returns data rows.

 

Figure 15: SELECT to generate a SELECT statement that returns data rows

The next step to adding parameterization is to define the SQL query statements used to access data. Because we only want to return the products that belong to the specified category, I re-use the SELECT statement in GetProducts (), but added a WHERE sub-sentence: WHERE CategoryID = @ CategoryID. The @ CategoryID parameter indicates to the TableAdapter Configuration Wizard that the method we are generating will need an input parameter for the corresponding class (that is, it can be a null-nullable integer.

 

Figure 16: enter a query that only returns the product of the specified category

In the last step of adding parameterization, we can select the data access mode and customize the name of the generated method. Corresponding to the Fill mode, let's change the name to FillByCategoryID. For the method (GetX method) that returns the able mode, let's use the name GetProductsByCategoryID.

 

Figure 17: select a name for the TableAdapter Method

After the Wizard is completed, the DataSet Designer contains the new TableAdapter methods.

 

Figure 18: Query products by category

Take some time to add a GetProductByProductID (productID) method in the same way.

These parameterized queries can be directly tested in the DataSet Designer. Right-click the method in TableAdapter and select "Preview Data )". Then, enter the value of the corresponding parameter and press "Preview )".

 

Figure 19: list of products in the beverage (Beverages) Category

Through the GetProductsByCategoryID (categoryID) method in Our DAL, we can design an asp.net webpage to display products belonging to the specified category. The following example shows all products belonging to the Beverages (beverage) Class (CategoryID = 1.

Beverages. aspx

Asp.net

 
 
  1.  < %@ Page Language="C#"   
  2.  
  3. AutoEventWireup="true" CodeFile="Beverages.aspx.cs"   
  4.  
  5. Inherits="Beverages" %>  
  6.  
  7. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   
  8.  
  9. Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  10.  
  11. < html xmlns="http://www.w3.org/1999/xhtml" >  
  12. < head runat="server">  
  13.     < title>Untitled Pagetitle>  
  14.     < link href="Styles.css"   
  15.  
  16. rel="stylesheet"   
  17.  
  18. type="text/css"   
  19.  
  20. />  
  21. < /head>  
  22. < body>  
  23.     < form id="form1" runat="server">  
  24.     < div>  
  25.         < h1>Beveragesh1>  
  26.         < p>  
  27.             < asp:GridView ID="GridView1" runat="server" 
  28.              CssClass="DataWebControlStyle">  
  29.                < HeaderStyle CssClass="HeaderStyle" />  
  30.                < AlternatingRowStyle CssClass="AlternatingRowStyle" />  
  31.             asp:GridView>  
  32.              < /p>  
  33.     < /div>  
  34.     < /form>  
  35. < /body>  
  36. < /html>  
  37.  

Beverages. aspx. cs

 
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using NorthwindTableAdapters;  
  12.  
  13. public partial class   
  14.  
  15. Beverages : System.Web.UI.Page  
  16. {  
  17.     protected void   
  18.  
  19. Page_Load(object sender, EventArgs e)  
  20.     {  
  21.         ProductsTableAdapter productsAdapter = new 
  22.          ProductsTableAdapter();  
  23.         GridView1.DataSource =  
  24.           productsAdapter.GetProductsByCategoryID(1);  
  25.         GridView1.DataBind();  
  26.     }  
  27. }  

 

Figure 20: display of all products in the Beverages (beverage) Category

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

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.