System.Data Namespace ConstraintCollection Class
Adds a constraint to the collection.
1. Adds the specified constraint to the collection.
2. Constructs a new UniqueConstraint, using the specified DataColumn, and adds it to the collection.
3. Constructs a new ForeignKeyConstraint, with the specified parent and child columns, and adds the constraint to the collection.
4. Constructs a new UniqueConstraint using the specified array of DataColumn objects, and adds it to the collection.
5. Constructs a new ForeignKeyConstraint, with the specified parent columns and child columns, and adds the constraint to the collection.
NOTE: This example uses one of the overloaded versions of Add. For other examples that may be available, see the individual overload topics.
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
|
|