av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

ASP.NET2.0數(shù)據(jù)教程:給DAL添加定制編碼

第六步:給DAL添加定制編碼

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設計、網(wǎng)站制作與策劃設計,寧洱網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設10多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:寧洱等地區(qū)。寧洱做網(wǎng)站價格咨詢:18980820575

添加到強類型DataSet中的TableAdapter和DataTable是在一個XML Schema定義文 件(Northwind.xsd)中定義的。你可以在解決方案資源管理器里在Northwind.xsd 文件上按右鼠標,選擇“查看編碼(View Code)”,打開這個Schema文件來查看其中內(nèi)容。

圖32:Northwinds強類型DataSet的XML Schema定義文件

這個schema信息在設計時編譯之后會被翻譯成C#或Visual Basic 編碼,或者如果有必要的話,會在運行時翻譯,然后你就能在調(diào)試器里單步遍歷執(zhí)行。想查看這些自動生成的編碼的話,在類視圖里,展開TableAdapter 類或者強類型的DataSet 類。如果在屏幕上看不到類視圖的話,在“查看”(View)菜單里選擇“ 類視圖”,或者按鍵組合Ctrl+Shift+C。在類視圖里,你能看到強類型的DataSet類和TableAdapter類的屬性,方法和事件。想看某個特定的方法的編碼話,在類視圖雙擊對應方法的名字或者在方法上按右鼠標,選擇“移至定義區(qū)(Go To Definition)”。

圖33:在類視圖里選擇“移至定義區(qū)(Go To Definition)”,查看自動生成的編碼

雖然自動生成的編碼省時省力,但這樣的編碼往往是非常通用化的(generic),為滿足一個應用程序特有的需求需要做些定制,就是所謂的定制編碼。但擴展自動生成的編碼的風險在于,如果生成這些編碼的工具決定該是重新生成這些編碼的時候了,則會把你定制的編碼沖掉。使用.NET 2.0中的一個新的部分(partial)類的概念,很容易將一個類的定義分寫在幾個文件里。這允許我們給自動生成的類添加我們自己的方法,屬性,和事件,而不用擔心Visual Studio會沖掉我們的定制編碼。

為示范如何定制DAL起見,讓我們來給SuppliersRow 添加一個GetProducts()方法。這個SuppliersRow類代表了Suppliers表的個別記錄,每個供應商(supplier)可以 提供0個到多個產(chǎn)品,所以GetProducts()將返回指定的供應商的這些產(chǎn)品。做法如下,在App_Code文件夾里添加一個新的類文件,將其命名為SuppliersRow.cs,然后在其中添加下列編碼:

C#

 
 
 
 
  1. using System;  
  2. using System.Data;  
  3. using NorthwindTableAdapters;  
  4.  
  5. public partial class   
  6.  
  7. Northwind  
  8. {  
  9.     public partial class   
  10.  
  11. SuppliersRow  
  12.     {  
  13.         public Northwind.ProductsDataTable GetProducts()  
  14.         {  
  15.             ProductsTableAdapter productsAdapter =  
  16.              new ProductsTableAdapter();  
  17.             return 
  18.               productsAdapter.GetProductsBySupplierID(this.SupplierID);  
  19.         }  
  20.     }  
  21. }  
  22.  

這個部分(partial)類指示編譯器在編譯Northwind.SuppliersRow類時,應該包含我們剛定義的這個GetProducts()方法。如果你編譯你的項目,然后返回類視圖,你就會看到GetProducts()已被列為Northwind.SuppliersRow的一個方法。

圖34: 定制編碼:GetProducts()方法成為Northwind.SuppliersRow類的一部分

GetProducts()方法現(xiàn)在就能用來枚舉一個指定供應商的產(chǎn)品列單,如下列編碼所示:

C#

 
 
 
 
  1. NorthwindTableAdapters.SuppliersTableAdapter   
  2.  
  3. suppliersAdapter = new   
  4.  
  5. NorthwindTableAdapters.SuppliersTableAdapter();  
  6.  
  7. // Get all of the suppliers  
  8. Northwind.SuppliersDataTable suppliers =  
  9.   suppliersAdapter.GetSuppliers();  
  10.  
  11. // Enumerate the suppliers  
  12. foreach (Northwind.SuppliersRow supplier in suppliers)  
  13. {  
  14.     Response.Write("Supplier: " +   
  15.  
  16. supplier.CompanyName);  
  17.     Response.Write("");  
  18.  
  19.     // List the products for this supplier  
  20.     Northwind.ProductsDataTable products = supplier.GetProducts();  
  21.     foreach (Northwind.ProductsRow product in products)  
  22.         Response.Write("" +   
  23.  
  24. product.ProductName + "");  
  25.  
  26.     Response.Write(" ");  
  27. }  
  28.  

This data can also be displayed in any of asp.net's data Web controls. The following page uses a GridView control with two fields:數(shù)據(jù)也可以在任何一種asp.net的Web控件中顯示。下面這個網(wǎng)頁 使用了含有2個字段的GridView 控件:

一個BoundField用以顯示每個供應商的名字,

另一個TemplateField,包含了一個BulletedList控件,用來綁定針對每個供應商調(diào)用 的GetProducts()方法返回的結果

我們將在以后的教程里討論怎樣來顯示這樣的主/從(master-detail)報表。在這里,這個例子的目的是用來示范如何使用添加到Northwind.SuppliersRow類中的自定義的方法的。

SuppliersAndProducts.aspx

asp.net

 
 
 
 
  1. < %@ Page Language="C#"   
  2.  
  3. AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"   
  4.  
  5. Inherits="SuppliersAndProducts" %>  
  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>  
  26.             Suppliers and Their Productsh1>  
  27.         < p>  
  28.             < asp:GridView ID="GridView1" runat="server" 
  29.              AutoGenerateColumns="False" 
  30.              CssClass="DataWebControlStyle">  
  31.                 < HeaderStyle CssClass="HeaderStyle" />  
  32.                 < AlternatingRowStyle CssClass="AlternatingRowStyle" />  
  33.                 < Columns>  
  34.                     < asp:BoundField DataField="CompanyName" 
  35.                       HeaderText="Supplier" />  
  36.                     < asp:TemplateField HeaderText="Products">  
  37.                         < ItemTemplate>  
  38.                             < asp:BulletedList ID="BulletedList1" 
  39.                              runat="server" DataSource="< %#  
  40.               ((Northwind.SuppliersRow)((System.Data.DataRowView)  
  41.               Container.DataItem).Row).GetProducts() %>"  
  42.                                     DataTextField="ProductName">  
  43.                             asp:BulletedList>  
  44.                         ItemTemplate>  
  45.                     asp:TemplateField>  
  46.                 Columns>  
  47.             asp:GridView>  
  48.              p>  
  49.  
  50.     div>  
  51.     form>  
  52. body>  
  53. html>  

SuppliersAndProducts.aspx.cs

C#

 
 
 
 
  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. SuppliersAndProducts : System.Web.UI.Page  
  16. {  
  17.     protected void   
  18.  
  19. Page_Load(object sender, EventArgs e)  
  20.     {  
  21.         SuppliersTableAdapter suppliersAdapter = new 
  22.           SuppliersTableAdapter();  
  23.         GridView1.DataSource = suppliersAdapter.GetSuppliers();  
  24.         GridView1.DataBind();  
  25.     }  
  26. }  
  27.  

圖 35: 定制編碼:供應商的公司名字列在左欄,他們的產(chǎn)品列在右欄

總結

構造web應用時,創(chuàng)建DAL應該是你最先做的步驟之一,應該在你開始創(chuàng)建表現(xiàn)層之前進行。使用Visual Studio的話,創(chuàng)建基于強類型DataSet的DAL是個可以不寫一行編碼,在10到15分鐘內(nèi)就可完成的任務。以后的教程將建立在這個DAL基礎之上。在下一個教程里,我們將定義一堆業(yè)務規(guī)則,然后看一下如何在一個分開的業(yè)務邏輯層里實現(xiàn)這些規(guī)則。


網(wǎng)頁名稱:ASP.NET2.0數(shù)據(jù)教程:給DAL添加定制編碼
網(wǎng)址分享:http://uogjgqi.cn/article/djpesde.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流