自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
本文介紹了在asp.net 2.0中如何創(chuàng)建一個數(shù)據(jù)訪問層(DAL)的第六步。

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

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

Northwinds強(qiáng)類型DataSet的XML Schema定義文件 

圖32:Northwinds強(qiáng)類型DataSet的XML Schema定義文件

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

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

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

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

為示范如何定制DAL起見,讓我們來給SuppliersRow 添加一個GetProducts()方法。這個SuppliersRow類代表了Suppliers表的個別記錄,每個供應(yīng)商(supplier)可以 提供0個到多個產(chǎn)品,所以GetProducts()將返回指定的供應(yīng)商的這些產(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類時,應(yīng)該包含我們剛定義的這個GetProducts()方法。如果你編譯你的項(xiàng)目,然后返回類視圖,你就會看到GetProducts()已被列為Northwind.SuppliersRow的一個方法。

GetProducts()方法成為Northwind.SuppliersRow類的一部分 

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

GetProducts()方法現(xiàn)在就能用來枚舉一個指定供應(yīng)商的產(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用以顯示每個供應(yīng)商的名字,

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

我們將在以后的教程里討論怎樣來顯示這樣的主/從(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.  

供應(yīng)商的公司名字列在左欄,他們的產(chǎn)品列在右欄 

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

總結(jié)

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

【編輯推薦】

  1. 如何在IIS6.0中部署asp.net mvc程序
  2. 用Winform傻瓜式搭建asp.net mvc框架
  3. ASP.NET Session失效的編程思路
  4. ASP.NET Session 狀態(tài)的存儲
  5. 了解ASP.NET Web應(yīng)用程序模型
責(zé)任編輯:book05 來源: 博客堂
相關(guān)推薦

2009-07-24 16:55:53

添加aspx頁面

2009-07-24 13:08:52

DataRowASP.NET 2.0

2009-07-24 13:08:03

BLL類ASP.NET 2.0

2009-07-24 17:08:31

添加站點(diǎn)地圖asp.net

2009-07-27 03:21:00

breadcrumb導(dǎo)

2009-07-27 09:28:55

TableAdapte

2009-07-27 03:23:00

Default.asp

2009-07-24 13:45:28

添加參數(shù)化

2009-07-27 08:51:24

ObjectDataS

2009-07-24 17:15:52

SiteMapData

2009-07-27 16:09:05

GridView顯示數(shù)

2009-07-27 09:35:57

業(yè)務(wù)邏輯層

2009-07-27 16:22:54

GridView選擇行

2009-07-27 09:01:44

ObjectDataS

2009-07-24 16:37:04

創(chuàng)建母版頁asp.net 2.0

2009-07-24 12:41:21

BLL類

2009-07-24 13:25:43

創(chuàng)建數(shù)據(jù)訪問層

2009-07-27 09:20:35

硬編碼參數(shù)

2010-06-25 08:51:46

ASP.NET MVC

2009-07-23 14:43:24

數(shù)據(jù)源控件ASP.NET 2.0
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號