Export (0) Print
Expand All

DataTable.Constraints Property

Gets the collection of constraints maintained by this table.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

'Declaration
Public ReadOnly Property Constraints As ConstraintCollection
'Usage
Dim instance As DataTable
Dim value As ConstraintCollection

value = instance.Constraints

/** @property */
public ConstraintCollection get_Constraints ()

public function get Constraints () : ConstraintCollection

Not applicable.

Property Value

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.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft