DataRelation
来源:互联网 发布:淘宝美工怎么样 编辑:程序博客网 时间:2024/04/28 08:56
DataRelation 的一项主要功能就是在 DataSet 中从一个 DataTable 浏览到另一个。它使您能够在给定相关 DataTable 中的单个 DataRow 的情况下检索一个 DataTable 中的所有相关 DataRow 对象。例如,当建立客户表和订单表之间的 DataRelation 后,可以使用 GetChildRows 检索特定客户行的所有订单行。
以下代码示例创建 DataSet 的 Customers 表和 Orders 表之间的 DataRelation,并返回每个客户的所有订单。
下一示例以上例为基础,将四个表关联在一起,并浏览这些关系。如上例所示,CustomerID 使 Customers 表与 Orders 表相关联。对于 Customers 表中的每个客户,将确定 Orders 表中的所有子行,以返回特定客户的订单数以及他们的 OrderID 值。
该扩展示例还将返回 OrderDetails 表和 Products 表中的值。Orders 表使用 OrderID 与 OrderDetails 表相关联,以确定在每一客户订单中订购的产品及数量。由于 OrderDetails 表只包含已订购产品的 ProductID,OrderDetails 将使用 ProductID 与 Products 相关联,以返回 ProductName。在这一关系中,Products 表为父表,而 Order Details 表为子表。因此,当循环访问 OrderDetails 表时,将调用 GetParentRow 来检索相关的 ProductName 值。
请注意,当为 Customers 表和 Orders 表创建 DataRelation 时,没有为 createConstraints 标志指定任何值(默认为 true)。它假定 Orders 表中的所有行都具有一个存在于父 Customers 表中的 CustomerID 值。如果 CustomerID 存在于 Customers 表之外的 Orders 表中,则 ForeignKeyConstraint 将引发异常。
如果子列可能包含父列不包含的值,添加 DataRelation 时请将 createConstraints 标志设置为 false。在该示例中,对于 Orders 表和 OrderDetails 表之间的 DataRelation,createConstraints 标志将设置为 false。这样,应用程序就可以返回 OrderDetails 表中的所有记录并只返回 Orders 表中记录的子集,而不会生成运行时异常。该扩展示例生成以下格式的输出。
复制代码
Customer ID: NORTS
Order ID: 10517
Order Date: 4/24/1997 12:00:00 AM
Product: Filo Mix
Quantity: 6
Product: Raclette Courdavault
Quantity: 4
Product: Outback Lager
Quantity: 6
Order ID: 11057
Order Date: 4/29/1998 12:00:00 AM
Product: Outback Lager
Quantity: 3以下代码示例是一个扩展示例,在该示例中将返回 OrderDetails 表和 Products 表中的值,并只返回 Orders 表中记录的子集。
DataRelation customerOrdersRelation =
customerOrders.Relations.Add("CustOrders"
,
customerOrders.Tables["Customers"
].Columns["CustomerID"
],
customerOrders.Tables["Orders"
].Columns["CustomerID"
]);
DataRelation orderDetailRelation =
customerOrders.Relations.Add("OrderDetail"
,
customerOrders.Tables["Orders"
].Columns["OrderID"
],
customerOrders.Tables["OrderDetails"
].Columns["OrderID"
], false
);
DataRelation orderProductRelation =
customerOrders.Relations.Add("OrderProducts"
,
customerOrders.Tables["Products"
].Columns["ProductID"
],
customerOrders.Tables["OrderDetails"
].Columns["ProductID"
]);
foreach
(DataRow custRow in
customerOrders.Tables["Customers"
].Rows)
{
Console.WriteLine("Customer ID: "
+ custRow["CustomerID"
]);
foreach
(DataRow orderRow in
custRow.GetChildRows(customerOrdersRelation))
{
Console.WriteLine(" Order ID: "
+ orderRow["OrderID"
]);
Console.WriteLine("/tOrder Date: "
+ orderRow["OrderDate"
]);
foreach
(DataRow detailRow in
orderRow.GetChildRows(orderDetailRelation))
{
Console.WriteLine("/t Product: "
+
detailRow.GetParentRow(orderProductRelation)["ProductName"
]);
Console.WriteLine("/t Quantity: "
+ detailRow["Quantity"
]);
}
}
转自http://hi.baidu.com/hooyan/blog/item/b8398286c182503966096ee9.html
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/langxiaodi/archive/2009/09/24/4587772.aspx
- DataRelation
- DataRelation
- DataRelation
- DataRelation
- DataRelation应用
- DataRelation 使用
- 使用DataRelation
- 初次使用DataRelation
- 添加 DataRelation (ADO.NET)
- DataRelation的用法(1)
- AOD.NET创建DataRelation对象.
- System.Data.DataRelation学习笔记
- Create DataRelation ship between two tables
- DataRelation 对象--多表填充DataSet
- DataRelation的用法(2),多表查询
- 相关表和 DataRelation 对象
- the differences of DataRelation class between 1.1 and 2.0
- DataList嵌套DataList(3. 使用DataRelation实现 纯代码)
- Web网站的性能测试工具
- VBA listview控件
- at91sam7x256之SPI0与SPILCD
- winxp与Ubuntu 9双系统之硬盘安装
- 关于“灵图天行者9 PC 导航伴侣零售版 破解版 ”不能用(需要重新注册)的解决办法
- DataRelation
- 今天来这里安家了
- 内存碎片产生原因及处理
- ASP.NET中防止页面多次提交的代码实现
- Enable sa account using sqlcmd
- 源码网站集合
- 大量并发的应急处理方案与实践2——使用缓存
- error and exeception
- 图片保存到数据库和从数据库读取图片并显示