System.Data Namespace ConstraintCollection Class
Constructs a new ForeignKeyConstraint, with the specified parent columns and child columns, and adds the constraint to the collection.
[ VB ]
Overridable Overloads Public Function Add ( _
ByVal name As String, _
ByVal primaryKeyColumns ( ) As DataColumn, _
ByVal foreignKeyColumns ( ) As DataColumn _
) As Constraint
[ C# ]
public virtual Constraint Add (
String name,
DataColumn [ ] primaryKeyColumns,
DataColumn [ ] foreignKeyColumns
);
[ C++ ]
public: virtual Constraint* Add (
String* name,
DataColumn* primaryKeyColumns [ ],
DataColumn* foreignKeyColumns [ ]
);
[ JScript ]
public function Add (
name : String,
primaryKeyColumns : DataColumn [ ],
foreignKeyColumns : DataColumn [ ]
) : Constraint
- name
- The name of the UniqueConstraint.
- primaryKeyColumns
- An array of DataColumn objects representing the primary key columns.
- foreignKeyColumns
- An array of DataColumn objects representing the foreign key columns.
A ForeignKeyConstraint and a UniqueConstraint are created automatically when you add a DataRelation to a DataSet. In that case, adding a second ForeignKeyConstraint on the same columns will result in an exception. To avoid this, use the ForeignKeyConstraint constructor to create the ForeignKeyConstraint and test it against existing collection members with the Equals method.
private void AddForeignConstraint ( DataSet myDataSet, DataTable myTable ) {
try {
DataColumn [ ] parentCols = new DataColumn [ 2 ];
DataColumn [ ] childCols = new DataColumn [ 2 ];
// get the tables from the DataSet.
DataTable tCustomers = myDataSet.Tables [ "Customers" ];
DataTable tOrders = myDataSet.Tables [ "Orders" ];
// set Columns.
parentCols [ 0 ] =tCustomers.Columns [ "id" ];
parentCols [ 1 ] =tCustomers.Columns [ "Name" ];
childCols [ 0 ] = tOrders.Columns [ "CustID" ];
childCols [ 1 ] = tOrders.Columns [ "CustName" ];
// create ForeignKeyConstraint
myTable.Constraints.Add ( "CustOrdersConstraint", parentCols, childCols );
}
catch ( Exception myException ) {
Response.Write ( myException.Message );
}
}
Private Sub AddForeignConstraint ( myDataSet As DataSet, myTable As DataTable )
Try
Dim parentCols ( 2 ) As DataColumn
Dim childCols ( 2 ) As DataColumn
' get the tables from the DataSet.
Dim tCustomers As DataTable = myDataSet.Tables ( "Customers" )
Dim tOrders As DataTable = myDataSet.Tables ( "Orders" )
' set Columns.
parentCols ( 0 ) = tCustomers.Columns ( "id" )
parentCols ( 1 ) = tCustomers.Columns ( "Name" )
childCols ( 0 ) = tOrders.Columns ( "CustID" )
childCols ( 1 ) = tOrders.Columns ( "CustName" )
' Create ForeignKeyConstraint
myTable.Constraints.Add ( "CustOrdersConstraint", parentCols, childCols )
Catch myException As Exception
Response.Write ( myException.Message )
End Try
End Sub |
|
C# |
VB |
ConstraintCollection Members ConstraintCollection.Add Overload List