DataTable.ChildRelations DataTable.ChildRelations DataTable.ChildRelations DataTable.ChildRelations Property

정의

DataTable에 대한 자식 관계 컬렉션을 가져옵니다.Gets the collection of child relations for this DataTable.

public:
 property System::Data::DataRelationCollection ^ ChildRelations { System::Data::DataRelationCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
[System.Data.DataSysDescription("DataTableChildRelationsDescr")]
public System.Data.DataRelationCollection ChildRelations { get; }
member this.ChildRelations : System.Data.DataRelationCollection
Public ReadOnly Property ChildRelations As DataRelationCollection

속성 값

테이블의 자식 관계가 포함된 DataRelationCollection입니다.A DataRelationCollection that contains the child relations for the table. DataRelation 개체가 없으면 빈 컬렉션이 반환됩니다.An empty collection is returned if no DataRelation objects exist.

예제

다음 예제에서는 합니다 ChildRelations 각 자식에 반환할 속성 DataRelationDataTable합니다.The following example uses the ChildRelations property to return each child DataRelation in a DataTable. 각 관계에 대 한 인수로 사용 됩니다는 GetChildRows 메서드는 DataRow 행의 배열을 반환 하도록 합니다.Each relation is then used as an argument in the GetChildRows method of the DataRow to return an array of rows. 행의 각 열 값이 출력 합니다.The value of each column in the row is then printed.

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();
    }
}
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

설명

DataRelation 두 테이블 간의 관계를 정의 합니다.A DataRelation defines the relationship between two tables. 일반적으로 두 테이블은 동일한 데이터를 포함 하는 단일 필드를 통해 연결 됩니다.Typically, two tables are linked through a single field that contains the same data. 예를 들어 주소 데이터를 포함 하는 테이블에는 국가/지역을 나타내는 코드를 포함 하는 단일 필드가 있을 수 있습니다.For example, a table which contains address data may have a single field containing codes that represent countries/regions. 국가/지역 데이터를 포함 하는 두 번째 테이블에서 국가/지역을 식별 하는 코드를 포함 하는 단일 필드로 있고이 코드가 첫 번째 테이블의 해당 필드에 삽입 됩니다.A second table that contains country/region data will have a single field that contains the code that identifies the country/region, and it is this code which is inserted into the corresponding field in the first table. DataRelation, 그런 다음 최소한 4 가지 정보를 포함 합니다. (1) 첫 번째 테이블의 이름, 첫 번째 테이블의 열 이름 (2), (3) 두 번째 테이블의 이름 및 (4) 두 번째 테이블의 열 이름입니다.A DataRelation, then, contains at least four pieces of information: (1) the name of the first table, (2) the column name in the first table, (3) the name of the second table, and (4) the column name in the second table.

적용 대상

추가 정보