DataTable.ChildRelations 属性
获取此 DataTable 的子关系的集合。
**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)
语法
声明
Public ReadOnly Property ChildRelations As DataRelationCollection
用法
Dim instance As DataTable
Dim value As DataRelationCollection
value = instance.ChildRelations
public DataRelationCollection ChildRelations { get; }
public:
property DataRelationCollection^ ChildRelations {
DataRelationCollection^ get ();
}
/** @property */
public DataRelationCollection get_ChildRelations ()
public function get ChildRelations () : DataRelationCollection
属性值
一个 DataRelationCollection,包含该表的子关系。如果 DataRelation 对象不存在,将返回空集合。
备注
DataRelation 定义两个表之间的关系。通常,两个表通过包含相同数据的单个字段链接起来。例如,包含地址数据的表可以拥有包含代表国家/地区的代码的单个字段。包含国家/地区数据的第二个表将拥有包含识别国家/地区的代码的单个字段,正是将此代码插入到第一个表的相应字段中。于是,DataRelation 至少包含以下四项信息:(1) 第一个表的名称;(2) 第一个表的列名;(3) 第二个表的名称;(4) 第二个表的列名。
示例
下面的示例使用 ChildRelations 属性返回 DataTable 中的每个子级 DataRelation。然后将每个关系用作 DataRow 的 GetChildRows 方法中的参数,以返回行的数组。然后输出该行中每列的值。
Public Sub GetChildRowsFromDataRelation()
' For each row in the table, get the child rows using the
' ChildRelations. For each item in the array, print the value
' of each column.
Dim table As DataTable = CreateDataSet().Tables("Customers")
Dim childRows() As DataRow
Dim relation as DataRelation
Dim row as DataRow
For Each relation In table.ChildRelations
For Each row In table.Rows
PrintRowValues(new DataRow() {row}, "Parent Row")
childRows = row.GetChildRows(relation)
' Print values of rows.
PrintRowValues(childRows, "child rows")
Next row
Next relation
End Sub
Public Function CreateDataSet() As DataSet
' create a DataSet with one table, two columns
Dim dataSet As DataSet
dataSet = new DataSet()
' create Customer table
Dim table As DataTable
table = new DataTable("Customers")
dataSet.Tables.Add(table)
table.Columns.Add("customerId", _
GetType(Integer)).AutoIncrement = true
table.Columns.Add("name", GetType(String))
table.PrimaryKey = new DataColumn() _
{ table.Columns("customerId") }
' create Orders table
table = new DataTable("Orders")
dataSet.Tables.Add(table)
table.Columns.Add("orderId", GetType(Integer)).AutoIncrement = true
table.Columns.Add("customerId", GetType(Integer))
table.Columns.Add("amount", GetType(Double))
table.PrimaryKey = new DataColumn() { table.Columns("orderId") }
' create relation
dataSet.Relations.Add(dataSet.Tables("Customers").Columns("customerId"), _
dataSet.Tables("Orders").Columns("customerId"))
' populate the tables
Dim orderId As Integer = 1
Dim customerId As Integer
Dim i As Integer
For customerId = 1 To 10
' add customer record
dataSet.Tables("Customers").Rows.Add( _
new object() { customerId, _
string.Format("customer{0}", customerId) })
' add 5 order records for each customer
For i = 1 To 5
dataSet.Tables("Orders").Rows.Add( _
new object() { orderId, customerId, orderId * 10 })
orderId = orderId+1
Next
Next
CreateDataSet = dataSet
End Function
private sub PrintRowValues(rows() As DataRow, label As String)
Console.WriteLine("\n{0}", label)
If rows.Length <= 0
Console.WriteLine("no rows found")
Exit Sub
End If
Dim row As DataRow
Dim column As DataColumn
For Each row In rows
For Each column In row.Table.Columns
Console.Write("\table {0}", row(column))
Next column
Console.WriteLine()
Next row
End Sub
private static void GetChildRowsFromDataRelation()
{
/* For each row in the table, get the child rows using the
ChildRelations. For each item in the array, print the value
of each column. */
DataTable table = CreateDataSet().Tables["Customers"];
DataRow[] childRows;
foreach(DataRelation relation in table.ChildRelations)
{
foreach(DataRow row in table.Rows)
{
PrintRowValues(new DataRow[] {row}, "Parent Row");
childRows = row.GetChildRows(relation);
// Print values of rows.
PrintRowValues(childRows, "child rows");
}
}
}
public static DataSet CreateDataSet()
{
// create a DataSet with one table, two columns
DataSet dataSet = new DataSet();
// create Customer table
DataTable table = new DataTable("Customers");
dataSet.Tables.Add(table);
table.Columns.Add("customerId", typeof(int)).AutoIncrement = true;
table.Columns.Add("name", typeof(string));
table.PrimaryKey = new DataColumn[] { table.Columns["customerId"] };
// create Orders table
table = new DataTable("Orders");
dataSet.Tables.Add(table);
table.Columns.Add("orderId", typeof(int)).AutoIncrement = true;
table.Columns.Add("customerId", typeof(int));
table.Columns.Add("amount", typeof(double));
table.PrimaryKey = new DataColumn[] { table.Columns["orderId"] };
// create relation
dataSet.Relations.Add(dataSet.Tables["Customers"].Columns["customerId"],
dataSet.Tables["Orders"].Columns["customerId"]);
// populate the tables
int orderId = 1;
for(int customerId=1; customerId<=10; customerId++)
{
// add customer record
dataSet.Tables["Customers"].Rows.Add(
new object[] { customerId,
string.Format("customer{0}", customerId) });
// add 5 order records for each customer
for(int i=1; i<=5; i++)
{
dataSet.Tables["Orders"].Rows.Add(
new object[] { orderId++, customerId, orderId * 10 });
}
}
return dataSet;
}
private static void PrintRowValues(DataRow[] rows, string label)
{
Console.WriteLine("\n{0}", label);
if(rows.Length <= 0)
{
Console.WriteLine("no rows found");
return;
}
foreach(DataRow row in rows)
{
foreach(DataColumn column in row.Table.Columns)
{
Console.Write("\table {0}", row[column]);
}
Console.WriteLine();
}
}
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
DataTable 类
DataTable 成员
System.Data 命名空间
ParentRelations
GetParentRows
GetChildRows