DataTable.ChildRelations プロパティ
この DataTable の子リレーションシップのコレクションを取得します。
Public ReadOnly Property ChildRelations As DataRelationCollection
[C#]
public DataRelationCollection ChildRelations {get;}
[C++]
public: __property DataRelationCollection* get_ChildRelations();
[JScript]
public function get ChildRelations() : DataRelationCollection;
プロパティ値
テーブルの子リレーションシップが格納されている DataRelationCollection 。 DataRelation オブジェクトが存在しない場合は null 値。
解説
DataRelation は 2 つのテーブル間のリレーションシップを定義します。通常、2 つのテーブルは、同じデータが格納されている単一のフィールドでリンクされています。たとえば、住所データが格納されているテーブルに、国や地域を表すコードが格納されている単一のフィールドがあるとします。国や地域のデータが格納されている 2 つ目のテーブルに、国や地域を識別するコードが格納されている単一のフィールドがあります。最初のテーブルの対応するフィールドに格納されているのがこのコードです。したがって、 DataRelation には、少なくとも (1) 最初のテーブルの名前、(2) 最初のテーブル内の列名、(3) 2 つ目のテーブルの名前、(4) 2 つ目のテーブル内の列名、という 4 種類の情報が格納されています。
使用例
[Visual Basic, C#, C++] 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 myTable As DataTable
myTable = CreateDataSet().Tables( "Customers" )
Dim childRows() As DataRow
Dim myRelation as DataRelation
Dim myRow as DataRow
For Each myRelation In myTable.ChildRelations
For Each myRow In myTable.Rows
PrintRowValues( new DataRow() {myRow}, "Parent Row" )
childRows = myRow.GetChildRows(myRelation)
' Print values of rows.
PrintRowValues( childRows, "child rows" )
Next myRow
Next myRelation
End Sub
Public Function CreateDataSet() As DataSet
' create a DataSet with one table, two columns
Dim myDataSet As DataSet
myDataSet = new DataSet()
' create Customer table
Dim t As DataTable
t = new DataTable( "Customers" )
myDataSet.Tables.Add( t )
t.Columns.Add( "customerId", GetType(Integer) ).AutoIncrement = true
t.Columns.Add( "name", GetType(String) )
t.PrimaryKey = new DataColumn() { t.Columns("customerId") }
' create Orders table
t = new DataTable( "Orders" )
myDataSet.Tables.Add( t )
t.Columns.Add( "orderId", GetType(Integer) ).AutoIncrement = true
t.Columns.Add( "customerId", GetType(Integer) )
t.Columns.Add( "amount", GetType(Double) )
t.PrimaryKey = new DataColumn() { t.Columns("orderId") }
' create relation
myDataSet.Relations.Add( myDataSet.Tables("Customers").Columns("customerId"), _
myDataSet.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
myDataSet.Tables("Customers").Rows.Add( _
new object() { customerId, string.Format("customer{0}", customerId) } )
' add 5 order records for each customer
For i = 1 To 5
myDataSet.Tables("Orders").Rows.Add( _
new object() { orderId, customerId, orderId * 10 } )
orderId = orderId+1
Next
Next
CreateDataSet = myDataSet
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 r As DataRow
Dim c As DataColumn
For Each r In rows
For Each c In r.Table.Columns
Console.Write( "\t {0}", r(c) )
Next c
Console.WriteLine()
Next r
End Sub
[C#]
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 myTable = CreateDataSet().Tables[ "Customers" ];
DataRow[] childRows;
foreach(DataRelation myRelation in myTable.ChildRelations)
{
foreach(DataRow myRow in myTable.Rows)
{
PrintRowValues( new DataRow[] {myRow}, "Parent Row" );
childRows = myRow.GetChildRows(myRelation);
// Print values of rows.
PrintRowValues( childRows, "child rows" );
}
}
}
public static DataSet CreateDataSet()
{
// create a DataSet with one table, two columns
DataSet myDataSet = new DataSet();
// create Customer table
DataTable t = new DataTable( "Customers" );
myDataSet.Tables.Add( t );
t.Columns.Add( "customerId", typeof(int) ).AutoIncrement = true;
t.Columns.Add( "name", typeof(string) );
t.PrimaryKey = new DataColumn[] { t.Columns["customerId"] };
// create Orders table
t = new DataTable( "Orders" );
myDataSet.Tables.Add( t );
t.Columns.Add( "orderId", typeof(int) ).AutoIncrement = true;
t.Columns.Add( "customerId", typeof(int) );
t.Columns.Add( "amount", typeof(double) );
t.PrimaryKey = new DataColumn[] { t.Columns["orderId"] };
// create relation
myDataSet.Relations.Add( myDataSet.Tables[ "Customers" ].Columns[ "customerId" ],
myDataSet.Tables[ "Orders" ].Columns[ "customerId" ] );
// populate the tables
int orderId = 1;
for( int customerId=1; customerId<=10; customerId++ )
{
// add customer record
myDataSet.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++ )
{
myDataSet.Tables[ "Orders" ].Rows.Add(
new object[] { orderId++, customerId, orderId * 10 } );
}
}
return myDataSet;
}
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 r in rows )
{
foreach( DataColumn c in r.Table.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
[C++]
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* myTable = CreateDataSet()->Tables->Item[ S"Customers" ];
DataRow* childRows[];
System::Collections::IEnumerator* myEnum = myTable->ChildRelations->GetEnumerator();
while (myEnum->MoveNext())
{
DataRelation* myRelation = __try_cast<DataRelation*>(myEnum->Current);
System::Collections::IEnumerator* myEnum1 = myTable->Rows->GetEnumerator();
while (myEnum1->MoveNext())
{
DataRow* myRow = __try_cast<DataRow*>(myEnum1->Current);
DataRow* temp0 [] = {myRow};
PrintRowValues( temp0, S"Parent Row" );
childRows = myRow->GetChildRows(myRelation);
// Print values of rows.
PrintRowValues( childRows, S"child rows" );
}
}
}
public:
static DataSet* CreateDataSet()
{
// create a DataSet with one table, two columns
DataSet* myDataSet = new DataSet();
// create Customer table
DataTable* t = new DataTable( S"Customers" );
myDataSet->Tables->Add( t );
t->Columns->Add( S"customerId", __typeof(int) )->AutoIncrement = true;
t->Columns->Add( S"name", __typeof(String) );
DataColumn* temp3 [] = {t->Columns->Item[S"customerId"]};
t->PrimaryKey = temp3;
// create Orders table
t = new DataTable( S"Orders" );
myDataSet->Tables->Add( t );
t->Columns->Add( S"orderId", __typeof(int) )->AutoIncrement = true;
t->Columns->Add( S"customerId", __typeof(int) );
t->Columns->Add( S"amount", __typeof(double) );
DataColumn* temp4 [] = {t->Columns->Item[S"orderId"]};
t->PrimaryKey = temp4;
// create relation
myDataSet->Relations->Add( myDataSet->Tables->Item[ S"Customers" ]->Columns->Item[ S"customerId" ],
myDataSet->Tables->Item[ S"Orders" ]->Columns->Item[ S"customerId" ] );
// populate the tables
int orderId = 1;
for( int customerId=1; customerId<=10; customerId++ )
{
// add customer record
Object* temp1 [] = {__box(customerId), String::Format(S"customer{0}", __box(customerId))};
myDataSet->Tables->Item[S"Customers"]->Rows->Add(
temp1 );
// add 5 order records for each customer
for( int i=1; i<=5; i++ )
{
Object* temp2 [] = {__box(orderId++), __box(customerId), __box(orderId * 10)};
myDataSet->Tables->Item[ S"Orders" ]->Rows->Add(
temp2 );
}
}
return myDataSet;
}
private:
static void PrintRowValues( DataRow* rows[], String* label )
{
Console::WriteLine( S"\n{0}", label );
if( rows->Length <= 0 )
{
Console::WriteLine( S"no rows found" );
return;
}
System::Collections::IEnumerator* myEnum2 = rows->GetEnumerator();
while (myEnum2->MoveNext())
{
DataRow* r = __try_cast<DataRow*>(myEnum2->Current);
System::Collections::IEnumerator* myEnum3 = r->Table->Columns->GetEnumerator();
while (myEnum3->MoveNext())
{
DataColumn* c = __try_cast<DataColumn*>(myEnum3->Current);
Console::Write( S"\t {0}", r->Item[c] );
}
Console::WriteLine();
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
DataTable クラス | DataTable メンバ | System.Data 名前空間 | ParentRelations | DataRelation | GetParentRows | GetChildRows