In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the method of adding parameterization to the data access layer in ASP.NET 2.0? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.
Step 3: add parameterization to the data access layer
At this point, ProductsTableAdapter has only one method, GetProducts (), which returns all the products in the database. It is certainly useful to be able to operate all products, but many times we want to get information about a given product, or all products that belong to a particular category. To add such functionality to our data access layer, we can add parameterized methods to TableAdapter.
Add parameterization: let's add a GetProductsByCategoryID (categoryID) method. To add a new method to DAL, let's go back to the DataSet designer, press the right mouse over ProductsTableAdapter, and select add query (Add Query).
Figure 14: press the right mouse on TableAdapter and select "add query"
The wizard will first ask us if we want to access the database through an ad-hoc SQL statement or by generating a new stored procedure or using an existing stored procedure. Let's choose to use the SQL statement. Next, the wizard will ask us what type of SQL query we use. Because we want to return all products that belong to the specified category, we need to write a SELECT statement that returns rows of data.
Figure 15: choose to generate a SELECT statement that returns a row of data
The next step in adding parameterization is to define the SQL query statement used to access the data. Because we only want to return those products that belong to the specified category, I reuse the SELECT statement in GetProducts (), but add a WHERE clause: WHERE CategoryID = @ CategoryID. The @ CategoryID parameter indicates to the TableAdapter configuration wizard that the method we are generating will require an input parameter of the corresponding class (that is, an integer that can be null-nullable).
Figure 16: enter a query that returns only the products of the specified category
In the step of adding parameterized *, we can choose which data access mode to use and customize the name of the generated method. Corresponding to the Fill pattern, let's change the name to FillByCategoryID, and for the method that returns the DataTable pattern (the GetX method), let's use the name GetProductsByCategoryID.
Figure 17: select a name for the TableAdapter method
At the end of the wizard, the DataSet designer includes these new TableAdapter methods.
Figure 18: querying products by classification
Take the time to add a GetProductByProductID (productID) method in the same way.
These parameterized queries can be tested directly in DataSet designer. Press the right mouse over the method in TableAdapter and select Preview data (Preview Data). Next, enter a value for the corresponding parameter, and then press Preview.
Figure 19: list of products belonging to the Beverage (Beverages) category
Through the GetProductsByCategoryID (categoryID) method in our DAL, we can design an asp.net page to display those products that belong to the specified category. The following example shows all the products that belong to the Beverages category (CategoryID=1).
Beverages.aspx
Asp.net
< %@ Page Language="C#" AutoEventWireup="true" CodeFile="Beverages.aspx.cs" Inherits="Beverages" %> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml" > < head runat="server"> < title>Untitled Pagetitle >
< link href="Styles.css" rel="stylesheet" type="text/css" /> < /head> < body> < form id="form1" runat="server"> < div> < h2>Beveragesh2 >
< p> < asp:GridView ID="GridView1" runat="server" CssClass="DataWebControlStyle"> < HeaderStyle CssClass="HeaderStyle" /> < AlternatingRowStyle CssClass="AlternatingRowStyle" />Asp:GridView >
< /p> < /div> < /form> < /body> < /html>Beverages.aspx.cs
Using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NorthwindTableAdapters; public partial class Beverages: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {ProductsTableAdapter productsAdapter = new ProductsTableAdapter () GridView1.DataSource = productsAdapter.GetProductsByCategoryID (1); GridView1.DataBind ();}}
All products that belong to the Beverages category are shown.
This is the answer to the question about what is the method of adding parameterization to the data access layer in ASP.NET 2.0. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.