DataTable.ChildRelations 屬性

定義

為這個 DataTable 取得子關聯的集合。

C#
[System.ComponentModel.Browsable(false)]
public System.Data.DataRelationCollection ChildRelations { get; }
C#
[System.ComponentModel.Browsable(false)]
[System.Data.DataSysDescription("DataTableChildRelationsDescr")]
public System.Data.DataRelationCollection ChildRelations { get; }

屬性值

DataRelationCollection,其中包含資料表的子關聯。 如果沒有任何 DataRelation 物件存在,則會傳回空的集合。

屬性

範例

下列範例會ChildRelations使用 屬性傳回 中的每個DataTable子系DataRelation。 然後,每個關聯都會當做 方法DataRow中的GetChildRows自變數,以傳回數據列的陣列。 然後列印數據列中每個數據行的值。

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

備註

定義 DataRelation 兩個數據表之間的關聯性。 一般而言,兩個數據表會透過包含相同數據的單一字段連結。 例如,包含地址數據的數據表可能有單一欄位,其中包含代表國家/地區的代碼。 包含國家/地區數據的第二個數據表會有單一欄位,其中包含可識別國家/地區的程式代碼,而此程式碼會插入第一個數據表的對應欄位。 DataRelation然後,至少包含四個資訊片段: (1) 第一個數據表的名稱, (2) 第一個數據表中的數據行名稱, (3) 第二個數據表的名稱, (4) 第二個數據表中的數據行名稱。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱