DataTable.ChildRelations プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataTable.ChildRelations プロパティの意味・解説 

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 オブジェクト存在しない場合、空のコレクション返されます。

解説解説

DataRelation2 つテーブル間のリレーションシップ定義します通常2 つテーブルは、同じデータ格納されている単一フィールドリンクされています。たとえば、住所データ格納されているテーブルに、国や地域を表すコード格納されている単一フィールドがあるとします。国や地域データ格納されている 2 つ目のテーブルに、国や地域識別するコード格納されている単一フィールドあります最初テーブル対応するフィールド格納されているのがこのコードです。したがってDataRelation には、少なくとも (1) 最初テーブルの名前、(2) 最初テーブル内の列名、(3) 2 つ目のテーブルの名前、(4) 2 つ目のテーブル内の列名、という 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();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataTable クラス
DataTable メンバ
System.Data 名前空間
ParentRelations
GetParentRows
GetChildRows
その他の技術情報
DataTable の作成使用


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataTable.ChildRelations プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataTable.ChildRelations プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataTable.ChildRelations プロパティ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataTable.ChildRelations プロパティ」の関連用語

DataTable.ChildRelations プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataTable.ChildRelations プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS