Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataTable Class
 Constraints Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DataTable..::.Constraints Property

Gets the collection of constraints maintained by this table.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public ReadOnly Property Constraints As ConstraintCollection
Visual Basic (Usage)
Dim instance As DataTable
Dim value As ConstraintCollection

value = instance.Constraints
C#
public ConstraintCollection Constraints { get; }
Visual C++
public:
property ConstraintCollection^ Constraints {
    ConstraintCollection^ get ();
}
JScript
public function get Constraints () : ConstraintCollection

Property Value

Type: System.Data..::.ConstraintCollection
A ConstraintCollection that contains the collection of Constraint objects for the table. An empty collection is returned 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.

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

Visual Basic
Private Sub CreateConstraint(dataSet As DataSet, _
    table1 As String, table2 As String, _
    column1 As String, column2 As String)

    Dim idKeyRestraint As ForeignKeyConstraint = _
        New ForeignKeyConstraint _
        (dataSet.Tables(table1).Columns(column1), _
        dataSet.Tables(table2).Columns(column2))

    ' 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

    dataSet.Tables(table1).Constraints.Add(idKeyRestraint)
    dataSet.EnforceConstraints = True
End Sub

C#
private void CreateConstraint(DataSet dataSet, 
    string table1, string table2, string column1, string column2)
{
    ForeignKeyConstraint idKeyRestraint = new 
        ForeignKeyConstraint(dataSet.Tables[table1].Columns[column1],
        dataSet.Tables[table2].Columns[column2]);

    // 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;

    dataSet.Tables[table1].Constraints.Add(idKeyRestraint);
    dataSet.EnforceConstraints = true;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker