asp.net.ph

DataTable.Constraints Property

System.Data Namespace   DataTable Class


Returns the collection of constraints maintained by this table.

Syntax


Script [ ConstraintCollection variable = ] DataTable.Constraints

Property Value


variable Returns a ConstraintCollection that contains the collection of Constraint objects for the table; otherwise a null value if no Constraint objects exist.

The property is read only with no default value.

Remarks

A ForeignKeyConstraint restricts the action performed when a value in a column ( or columns ) is either deleted or updated. Such a constraint is intended to be used with primary key columns. In a parent/child relationship between two tables, deleting a value from the parent table can affect the child rows in one of the following ways.

  • The child rows can also be deleted ( a cascading action ).
  • The values in the child column ( or columns ) can be set to null values.
  • The values in the child column ( or columns ) can be set to default values.
  • An exception can be generated.

A UniqueConstraint becomes active when attempting to set a value in a primary key to a non-unique value.

Example

The following example adds a ForeignKeyConstraint to the collection of constraints.

Private void ConstraintsDemo ( DataSet myDataSet, string table1, 
      string table2, string column1, string column2 ) {
   ForeignKeyConstraint idKey = new ForeignKeyConstraint
      ( myDataSet.Tables [ Table1 ].Columns [ Column1 ],
      myDataSet.Tables [ Table2 ].Columns [ Column2 ] );
   // set null values when a value is deleted.
   idKey.DeleteRule = Rule.SetNull;
   idKey.UpdateRule = Rule.Cascade;
   // set AcceptRejectRule to cascade changes.
   idKey.AcceptRejectRule = AcceptRejectRule.Cascade;

   myDataSet.Tables [ Table1 ].Constraints.Add ( idKey );
   myDataSet.EnforceConstraints = true;
}
  C# VB

See Also

DataTable Members   AcceptRejectRule   ForeignKeyConstraint   UniqueConstraint   Rule 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