首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 软件频道>ASP.NET2.0数据操作之创建数据访问层

ASP.NET2.0数据操作之创建数据访问层

2006-07-23 07:00作者:蒋涛出处:博客园责任编辑:方舟


  给DAL添加定制编码

  添加到强类型DataSet中的TableAdapter和DataTable是在一个XML Schema定义文 件(Northwind.xsd)中定义的。你可以在解决方案资源管理器里在Northwind.xsd 文件上按右鼠标,选择“查看编码(View Code)”,打开这个Schema文件来查看其中内容。


图32:Northwinds强类型DataSet的XML Schema定义文件

  这个schema信息在设计时编译之后会被翻译成C#或Visual Basic 编码,或者如果有必要的话,会在运行时 翻译,然后你就能在调试器里单步遍历执行。想查看这些自动生成的编码的话,在类视图里,展 开TableAdapter 类或者强类型的DataSet 类。如果在屏幕上看不到类视图的话,在“查看”(View)菜单里选择“ 类视图”,或者按键组合Ctrl+Shift+C。在类视图里,你能看到强类型的DataSet类和TableAdapter类的属性,方法和事件。想看某个特定的方法的编码话,在类视图双击对应方法的名字或者在方法上按右鼠标,选 择“移至定义区(Go To Definition)”。


图33:在类视图里选择“移至定义区(Go To Definition)”,查看自动生成的编码

  虽然自动生成的编码省时省力,但这样的编码往往是非常通用化的(generic),为满足一个应用程序特有的需 求需要做些定制。但扩展自动生成的编码的风险在于,如果生成这些编码的工具决定该是重新生成这些编码的 时候了,则会把你定制的编码冲掉。使用.NET 2.0中的一个新的部分(partial)类的概念,很容易将一个类的 定义分写在几个文件里。这允许我们给自动生成的类添加我们自己的方法,属性,和事件,而不用担心Visual Studio会冲掉我们的定制编码。

  为示范如何定制DAL起见,让我们来给SuppliersRow 添加一个GetProducts()方法。这 个SuppliersRow类代表了Suppliers表的个别记录,每个供应商(supplier)可以 提供0个到多个产品,所以GetProducts()将返回指定的供应商的这些产品。做法如 下,在App_Code文件夹里添加一个新的类文件,将其命名为SuppliersRow.cs, 然后在其中添加下列编码:

C#
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
using System; 
using System.Data; 
using NorthwindTableAdapters; 

public partial class  

Northwind 
{ 
    public partial class  

SuppliersRow 
    { 
        public Northwind.ProductsDataTable GetProducts() 
        { 
            ProductsTableAdapter productsAdapter = 
             new ProductsTableAdapter(); 
            return 
              productsAdapter.GetProductsBySupplierID(this.SupplierID); 
        } 
    } 
} 

  这个部分(partial)类指示编译器在编译Northwind.SuppliersRow类时,应该包含我们刚定义的这个GetProducts()方法。如果你编译你的项目,然后返回类视图,你就会看到GetProducts()已被列为Northwind.SuppliersRow的一个方法。


图34: GetProducts()方法成为Northwind.SuppliersRow类的一部 分

  GetProducts()方法现在就能用来枚举一个指定供应商的产品列单,如下列编码所示:

C#
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
NorthwindTableAdapters.SuppliersTableAdapter  

suppliersAdapter = new  

NorthwindTableAdapters.SuppliersTableAdapter(); 

// Get all of the suppliers 
Northwind.SuppliersDataTable suppliers = 
  suppliersAdapter.GetSuppliers(); 

// Enumerate the suppliers 
foreach (Northwind.SuppliersRow supplier in suppliers) 
{ 
    Response.Write("Supplier: " +  

supplier.CompanyName); 
    Response.Write("<ul>"); 

    // List the products for this supplier 
    Northwind.ProductsDataTable products = supplier.GetProducts(); 
    foreach (Northwind.ProductsRow product in products) 
        Response.Write("<li>" +  

product.ProductName + "</li>"); 

    Response.Write("</ul><p> </p>"); 
} 

  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:数据也可以在任何一种ASP.NET的Web控件中显示。下面这个网页 使用了含有2个字段的GridView 控件:
  • 一个BoundField用以显示每个供应商的名字,
  • 另一个TemplateField,包含了一个BulletedList控件,用来绑定针对每个供应商调用 的GetProducts()方法返回的结果
  我们将在以后的教程里讨论怎样来显示这样的主/从(master-detail)报表。在这里,这个例子的目的是用 来示范如何使用添加到Northwind.SuppliersRow类中的自定义的方法的。

SuppliersAndProducts.aspx
ASP.NET
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
<%@ Page Language="C#"  

AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"  

Inherits="SuppliersAndProducts" %> 

<!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 Page</title> 
    <link href="Styles.css"  

rel="stylesheet"  

type="text/css"  

/> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <h1> 
            Suppliers and Their Products</h1> 
        <p> 
            <asp:GridView ID="GridView1" runat="server" 
             AutoGenerateColumns="False" 
             CssClass="DataWebControlStyle"> 
                <HeaderStyle CssClass="HeaderStyle" /> 
                <AlternatingRowStyle CssClass="AlternatingRowStyle" /> 
                <Columns> 
                    <asp:BoundField DataField="CompanyName" 
                      HeaderText="Supplier" /> 
                    <asp:TemplateField HeaderText="Products"> 
                        <ItemTemplate> 
                            <asp:BulletedList ID="BulletedList1" 
                             runat="server" DataSource="<%# 
              ((Northwind.SuppliersRow)((System.Data.DataRowView) 
              Container.DataItem).Row).GetProducts() %>" 
                                    DataTextField="ProductName"> 
                            </asp:BulletedList> 
                        </ItemTemplate> 
                    </asp:TemplateField> 
                </Columns> 
            </asp:GridView> 
             </p> 

    </div> 
    </form> 
</body> 
</html> 

SuppliersAndProducts.aspx.cs
C#
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
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  

SuppliersAndProducts : System.Web.UI.Page 
{ 
    protected void  

Page_Load(object sender, EventArgs e) 
    { 
        SuppliersTableAdapter suppliersAdapter = new 
          SuppliersTableAdapter(); 
        GridView1.DataSource = suppliersAdapter.GetSuppliers(); 
        GridView1.DataBind(); 
    } 
} 


图 35: 供应商的公司名字列在左栏,他们的产品列在右栏

  总结

  构造web应用时,创建DAL应该是你最先做的步骤之一,应该在你开始创建表现层之前进行。使用Visual Studio的话,创建基于强类型DataSet的DAL是个可以不写一行编码,在10到15分钟内就可完成的任务。以后的 教程将建立在这个DAL基础之上。在下一个教程里,我们将定义一堆业务规则,然后看一下如何在一个分开的 业务逻辑层里实现这些规则。

关注此文的读者还看过:

返回软件频道首页

共10页。 上一页12345678910

软件频道最新更新

热点推荐

天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络