首页 > 代码库 > DataSet DataRelation

DataSet DataRelation

 

DataTable.ChildRelations

Gets the collection of child relations for this DataTable.

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. 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.

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

 DataRow.GetParentRows Method

DataRow.GetChildRows Method

DataTable.Constraints Property

private void CreateConstraint(DataSet dataSet,     string table1, string table2, string column1, string column2){    ForeignKeyConstraint idKeyRestraint = new         ForeignKeyConstraint(dataSet.Tables[table1].Columns[column1],        dataSet.Tables[table2].Columns[column2]);    // Set null values when a value is deleted.    idKeyRestraint.DeleteRule = Rule.SetNull;    idKeyRestraint.UpdateRule = Rule.Cascade;    // Set AcceptRejectRule to cascade changes.    idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade;    dataSet.Tables[table1].Constraints.Add(idKeyRestraint);    dataSet.EnforceConstraints = true;}
View Code

 

DataTable Constraints

http://msdn.microsoft.com/en-us/library/st1t2c35(v=vs.110).aspx

ForeignKeyConstraint

Rule setting

Description

Cascade                    

Delete or update related rows.

SetNull                    

Set values in related rows to DBNull.

SetDefault                    

Set values in related rows to the default value.

None                    

Take no action on related rows. This is the default.

UniqueConstraint