This documentation is archived and is not being maintained.

DataTable.Constraints Property

Gets the collection of constraints maintained by this table.

[Visual Basic]
Public ReadOnly Property Constraints As ConstraintCollection
public ConstraintCollection Constraints {get;}
public: __property ConstraintCollection* get_Constraints();
public function get Constraints() : ConstraintCollection;

Property Value

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


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.


[Visual Basic, C#, C++] The following example adds a ForeignKeyConstraint to the collection of constraints.

[Visual Basic] 
Private Sub CreateConstraint(myDataSet As DataSet, table1 As String, table2 As String, _
                                                   column1 As String, column2 As String)
   Dim idKeyRestraint As ForeignKeyConstraint = New ForeignKeyConstraint _
        (myDataSet.Tables(table1).Columns(column1), _
   ' Set null values when a value is deleted.
   idKeyRestraint.DeleteRule = Rule.SetNull
   idKeyRestraint.UpdateRule = Rule.Cascade
   ' Set AcceptRejectRule to cascade changes.
   idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade

   myDataSet.EnforceConstraints = True
End Sub

private void CreateConstraint(DataSet myDataSet, string table1, string table2, string column1, string column2)
   ForeignKeyConstraint idKeyRestraint = new ForeignKeyConstraint(myDataSet.Tables[table1].Columns[column1],
   // Set null values when a value is deleted.
   idKeyRestraint.DeleteRule = Rule.SetNull;
   idKeyRestraint.UpdateRule = Rule.Cascade;
   // Set AcceptRejectRule to cascade changes.
   idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade;

   myDataSet.EnforceConstraints = true;

 void CreateConstraint(DataSet* myDataSet, String* table1, String* table2, String* column1, String* column2)
    ForeignKeyConstraint* idKeyRestraint = new ForeignKeyConstraint(myDataSet->Tables->Item[table1]->Columns->Item[column1],
    // Set null values when a value is deleted.
    idKeyRestraint->DeleteRule = Rule::SetNull;
    idKeyRestraint->UpdateRule = Rule::Cascade;
    // Set AcceptRejectRule to cascade changes.
    idKeyRestraint->AcceptRejectRule = AcceptRejectRule::Cascade;
    myDataSet->EnforceConstraints = true;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace | AcceptRejectRule | ForeignKeyConstraint | UniqueConstraint | Rule