DataRelation 的主要功能之一是允许您在 DataSet 中从一个 DataTable 导航至另一个 DataTable。它使您能够在给定相关 DataTable 中的单个 DataRow 的情况下检索一个 DataTable 中的所有相关 DataRow 对象。例如,当建立客户表和订单表之间的 DataRelation 后,可以使用 DataRow.GetChildRows 检索特定客户行的所有订单行。
以下代码示例创建 DataSet 中 Customers 表和 Orders 表之间的 DataRelation,并返回每个客户的所有订单。
[Visual Basic]
Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
custDS.Tables("Customers").Columns("CustomerID"), _
custDS.Tables("Orders").Columns("CustomerID")) Dim custRow As DataRow
Dim orderRow As DataRowFor Each custRow in custDS.Tables("Customers").Rows
Console.WriteLine(custRow("CustomerID"))
For Each orderRow in custRow.GetChildRows(custOrderRel)
Console.WriteLine(orderRow("OrderID"))
Next
Next
[C#]
DataRelation custOrderRel = custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custRow in custDS.Tables["Customers"].Rows)
{
Console.WriteLine(custRow["CustomerID"]);
foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
Console.WriteLine(orderRow["OrderID"]);
}
以下代码示例创建 DataSet 中 Customers 表和 Orders 表之间的 DataRelation,并返回每个客户的所有订单。
[Visual Basic]
Dim custOrderRel As DataRelation = custDS.Relations.Add("CustOrders", _
custDS.Tables("Customers").Columns("CustomerID"), _
custDS.Tables("Orders").Columns("CustomerID")) Dim custRow As DataRow
Dim orderRow As DataRowFor Each custRow in custDS.Tables("Customers").Rows
Console.WriteLine(custRow("CustomerID"))
For Each orderRow in custRow.GetChildRows(custOrderRel)
Console.WriteLine(orderRow("OrderID"))
Next
Next
[C#]
DataRelation custOrderRel = custDS.Relations.Add("CustOrders",
custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custRow in custDS.Tables["Customers"].Rows)
{
Console.WriteLine(custRow["CustomerID"]);
foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel))
Console.WriteLine(orderRow["OrderID"]);
}
在.NET Framework SDK文档里有这个属性相关方法的参数说明
我有一个你所要实现功能的pdf文档,请留下Email,我传给你