asp.net.ph

ConstraintCollection.Add Method ( String, DataColumn [ ], DataColumn [ ] )

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

Parameters

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.

Remarks

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.

Example


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 );
   }
}
  C# VB

See Also

ConstraintCollection Members   ConstraintCollection.Add Overload List Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph