您的位置:首页 > 其它

轻松玩转Typed DataSet, Part I

2004-12-08 04:11 429 查看

轻松玩转Typed DataSet, Part I

Written by: Rickie Lee

Dec. 07, 2004

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

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

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

3. 每一个DataTable是Typed DataSet的一个属性.

4. 同样地,每一个Field是Data 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文件不仅包含table和column名称,而且包含keys, relationships和constraints信息。

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

下面以Northwind Database为例:

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

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

从Toolbox中拖拉一个新的Relation到XSD编辑界面,并弹出如下Edit Relation的windows form:



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

另外,也可以通过Toolbox和XSD的编辑界面,增加/修改table中column元素。



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

在 Schema(架构)菜单中,单击 Generate DataSet(生成 DataSet)或Preview DataSet。

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

public class OrderDataSet : DataSet

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

二、使用Typed DataSet

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

如下是Northwind Database中Orders 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,还有一个名为Orders的DataRowCollection Class(OrderDataSet.Orders)。

调用上述Typed DataSet的Code 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 Class(Microsoft Data Access Application Block)的FillDataset方法,来完成DataSet的填充。FillDataSet方法支持Typed DataSet,值得推荐使用。

Any questions or errors, please leave comments. Thanks.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: