[ Team LiB ] |
Recipe 6.8 Creating Constraints, PrimaryKeys, Relationships Based on Multiple ColumnsProblemYou need to create a constraint, primary key, or a relationship between two tables in a DataSet using more than one column. SolutionUse the System.Data.UniqueConstraint and System.Data.ForeignKeyConstraint types. The sample code creates a DataSet containing two tables: Parent and Child. A multicolumn unique constraint and primary key are created for the Parent table. A multicolumn foreign-key constraint is created on the Child table. Finally, a DataRelation between the primary key in the Parent table and the foreign key in the Child table is created. The C# code is shown in Example 6-26. Example 6-26. File: MultiColumnConstraintAndRelationForm.cs// Namespaces, variables, and constants using System; using System.Text; using System.Data; // . . . StringBuilder result = new StringBuilder( ); DataSet ds = new DataSet( ); // Create the parent table. result.Append("Creating parent table." + Environment.NewLine); DataTable dtParent = new DataTable("Parent"); DataColumnCollection pCols = dtParent.Columns; pCols.Add("ParentKey1", typeof(Int32)); pCols.Add("ParentKey2", typeof(Int32)); pCols.Add("ParentData1", typeof(String)); pCols.Add("ParentData2", typeof(String)); // Set the multicolumn unique constraint. result.Append("Creating unique constraint on parent table." + Environment.NewLine); dtParent.Constraints.Add(new UniqueConstraint("UConstraint", new DataColumn[] {pCols["ParentKey1"], pCols["ParentKey2"]}, false)); // Set the multicolumn primary key. result.Append("Creating primary key on parent table." + Environment.NewLine); dtParent.PrimaryKey = new DataColumn[] {pCols["ParentKey1"], pCols["ParentKey2"]}; // Add the parent table to the DataSet. ds.Tables.Add(dtParent); // Create the child table. result.Append("Creating child table." + Environment.NewLine); DataTable dtChild = new DataTable("Child"); DataColumnCollection cCols = dtChild.Columns; cCols.Add("ChildIndex1", typeof(Int32)).Unique = true; cCols.Add("ParentKey1", typeof(Int32)); cCols.Add("ParentKey2", typeof(Int32)); cCols.Add("ChildData1", typeof(String)); cCols.Add("ChildData2", typeof(String)); ds.Tables.Add(dtChild); // Set the foreign-key constraint. result.Append("Creating foreign key contraint." + Environment.NewLine); dtChild.Constraints.Add("FKConstraint", new DataColumn[] {pCols["ParentKey1"], pCols["ParentKey2"]}, new DataColumn[] {cCols["ParentKey1"], cCols["ParentKey2"]}); // Create the relation between parent and child. result.Append("Creating relationship." + Environment.NewLine); ds.Relations.Add("Relation", new DataColumn[] {dtParent.Columns["ParentKey1"], dtParent.Columns["ParentKey2"]}, new DataColumn[] {dtChild.Columns["ParentKey1"], dtChild.Columns["ParentKey2"]}, true); resultTextBox.Text = result.ToString( ) + Environment.NewLine + "Done."; DiscussionUnique and foreign-key constraintsThe System.Data.Constraint class is an abstract class that is the base class for the two .NET Framework built-in classes:
By default, a UniqueConstraint is created on the parent table and a ForeignKeyConstraint is created on the child table when a DataRelation object is created relating the two tables. Constraints are only enforced if the EnforceConstraints property is true. The Add( ) method of the ConstraintCollection of the DataTable, which is accessed using the Constraints property, has two overloads that you can use to add a unique constraint to a table. The prototypes are: public void Add(String name, DataColumn col, Boolean isPrimaryKey); public void Add(String name, DataColumn[] cols, Boolean isPrimaryKey); The arguments are:
There are two methods that you can use to add a foreign key constraint to a table. The prototypes are: public void Add(String name, DataColumn parentCol, DataColumn childCol); public void Add(String name, DataColumn[] parentCols, DataColumn childCols); The arguments are:
There is also one method that you can use to add either a unique constraint or a foreign key constraint to a table. The prototype is: public void Add(Constraint constraint); The argument is:
PrimaryKeyThe PrimaryKey property of the DataTable is used to set or get the primary key for a table. The prototypes are: DataColumn[] pka = DataTable.PrimaryKey; DataTable.PrimaryKey = pka; The argument is:
DataRelationThe Add( ) method of the DataRelationCollection of the DataSet, which is accessed using the Relations property, has seven overloads that you can use to add a relationship between two tables to the DataSet. The prototypes are: public void Add(DataRelation dr); public virtual DataRelation Add(DataColumn parentCol, DataColumn childCol); public virtual DataRelation Add(DataColumn[] parentCols, DataColumn[] childCols); public virtual DataRelation Add(String name, DataColumn parentCol, DataColumn childCol); public virtual DataRelation Add(String name, DataColumn[] parentCols, DataColumn[] childCols); public virtual DataRelation Add(String name, DataColumn parentCol, DataColumn childCol, Boolean createFKConstraint); public virtual DataRelation Add(String name, DataColumn[] parentCols, DataColumn[] childCols, Boolean createFKConstraint); The arguments are:
|
[ Team LiB ] |