• 轻松玩转Typed DataSet, Part I


    轻松玩转Typed DataSet, Part I

     

    Written by: Rickie Lee

    Dec. 07, 2004

    Typed DataSet与一般DataSet的简单比较:

    1. Typed DataSet直接继承DataSet Class, 是一个特殊的DataSet类型.

    2. 通过Typed DataSet,可以方便直接地获取tablesfilelds.

    3. 每一个DataTableTyped DataSet的一个属性.

    4. 同样地,每一个FieldData Row的一个属性.

    5. 使用Typed DataSet,不仅语法简洁,而且在编译期间进行类型检查。

    关于Typed DataSet的优点与缺点,及其在分布式应用系统中作用,请参考《分布式应用架构中的数据传输对象(DTO》。

     

    一、创建Typed DataSet

    这里演示 XSD 架构文件创建有类型的 DataSet过程。要使用 Visual Studio .NET XSD 架构文件创建有类型的 DataSet,请执行以下步骤:

    1. Visual Studio .NET中,创建一个新项目或打开一个现有项目。

    2. 为项目添加一个现有的 XSD 架构,或在组件设计器中创建一个新的 XSD 架构。

     

    从上图可以发现,Typed DataSet的后缀为.xsd,因为Typed DataSet的源文件是XML Schema文档。XSD文件不仅包含tablecolumn名称,而且包含keys, relationshipsconstraints信息。

     

    3. 使用Server Explorer打开相应的Database,然后通过拖拉tables到上述XSD文件。

    下面以Northwind Database为例:

    添加Orders, Order Details两个表。VS.Net IDE不能自动检测Database中表间关系,因此OrdersOrder Details两个表之间在XSD文件没有自动产生任何关系。

     

    4. 手工建立表与表之间的关系

    Toolbox中拖拉一个新的RelationXSD编辑界面,并弹出如下Edit Relationwindows form

    通过上述Edit Relation界面,可以编辑relationship参数。

    另外,也可以通过ToolboxXSD的编辑界面,增加/修改tablecolumn元素。

     

    注意:示例中将上面的Order Details元素改名为OrderDetails,去除其中的空格,以免一些不必要的麻烦。

     

    Schema(架构)菜单中,单击 Generate DataSet(生成 DataSetPreview DataSet

    为确认已创建该有类型的 DataSet,可以在解决方案资源管理器中单击 Show All Files(显示所有文件)按钮。展开 XSD 架构文件的节点,确认存在一个与 XSD 架构相关联的代码文件。该代码文件定义了新的有类型的 DataSet 类。

    public class OrderDataSet : DataSet

    …… Typed DataSet/OrderDataSet直接继承DataSet Class.

     

    二、使用Typed DataSet

    在使用上述Typed DataSet之前,先了解OrderDataSet.XSD文件内容。

    如下是Northwind DatabaseOrders table的架构元素:

    <xs:element name="Orders">

    <xs:complexType>

             <xs:sequence>

             <xs:element name="OrderID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />

             <xs:element name="CustomerID" type="xs:string" minOccurs="0" />

             <xs:element name="EmployeeID" type="xs:int" minOccurs="0" />

             <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />

             <xs:element name="RequiredDate" type="xs:dateTime" minOccurs="0" />

             <xs:element name="ShippedDate" type="xs:dateTime" minOccurs="0" />

             <xs:element name="ShipVia" type="xs:int" minOccurs="0" />

             <xs:element name="Freight" type="xs:decimal" minOccurs="0" />

             <xs:element name="ShipName" type="xs:string" minOccurs="0" />

             <xs:element name="ShipAddress" type="xs:string" minOccurs="0" />

             <xs:element name="ShipCity" type="xs:string" minOccurs="0" />

             <xs:element name="ShipRegion" type="xs:string" minOccurs="0" />

             <xs:element name="ShipPostalCode" type="xs:string" minOccurs="0" />

             <xs:element name="ShipCountry" type="xs:string" minOccurs="0" />

             </xs:sequence>

    </xs:complexType>

    </xs:element>

    上述XML Schema会生成OrdersRow这一对象名称OrderDataSet.OrdersRow,还有一个名为OrdersDataRowCollection ClassOrderDataSet.Orders)。

     

    调用上述Typed DataSetCode snippet

    OrderDataSet theOrderDS = new OrderDataSet();

    string strSelectOrders = "Select  * From Orders ";

    strSelectOrders += "Select * From [Order Details]";

    SqlHelper.FillDataset(connStr, CommandType.Text, strSelectOrders, theOrderDS, new string[] {"Orders", "OrderDetails"});

     

    StringBuilder strResults = new StringBuilder();

     

    foreach(OrderDataSet.OrdersRow theOrder in theOrderDS.Orders)

    {

             strResults.Append(theOrder.OrderID.ToString() + " "

                      + theOrder.CustomerID.ToString() + " "

                      + theOrder.EmployeeID.ToString() + Environment.NewLine);

             strResults.Append("Order Details: ");

             strResults.Append(theOrder.GetChildRows("OrdertoOrderDetails").GetLength(0).ToString());

             strResults.Append(Environment.NewLine);

    }

     

    txtResults.Text = strResults.ToString();

     

    代码比较简单,上述代码调用了SqlHelper ClassMicrosoft Data Access Application Block)的FillDataset方法,来完成DataSet的填充。FillDataSet方法支持Typed DataSet,值得推荐使用。

     

    Any questions or errors, please leave comments. Thanks.

     

  • 相关阅读:
    Vijos P1459 车展 (treap 任意区间中位数)
    「BZOJ1691」[Usaco2007 Dec] 挑剔的美食家 (Treap)
    hdu 1540 Tunnel Warfare(Treap)
    hdu 2844 Coins (多重背包+二进制优化)
    hdu 2159 FATE (二维完全背包)
    hdu 2955 Robberies (01背包)
    hdu 2546 饭卡 (01背包)
    hdu 2191 (多重背包二进制优化)
    2019西北工业大学程序设计创新实践基地春季选拔赛 I Chino with Rewrite (并查集+树链剖分+线段树)
    CF895E Eyes Closed (期望)
  • 原文地址:https://www.cnblogs.com/rickie/p/74161.html
Copyright © 2020-2023  润新知