Export (0) Print
Expand All

ConstraintCollection Class

Represents a collection of constraints for a DataTable.

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

public sealed class ConstraintCollection : InternalDataCollectionBase

The ConstraintCollection is accessed through the DataTable.Constraints property.

The ConstraintCollection can contain both UniqueConstraint and ForeignKeyConstraint objects for the DataTable. A UniqueConstraint object makes sure that data in a specific column is always unique to preserve the data integrity. The ForeignKeyConstraint determines what will occur in related tables when data in the DataTable is either updated or deleted. For example, if a row is deleted, the ForeignKeyConstraint will determine whether the related rows are also deleted (a cascade), or some other course of action.


When you add a DataRelation that creates a relationship between two tables to a DataSet, both a ForeignKeyConstraint and a UniqueConstraint are created automatically. The UniqueConstraint is applied to the primary key column in the parent DataTable, and the constraint is added to that table's ConstraintCollection. The ForeignKeyConstraint is applied to the primary key column and the foreign key column, and the constraint is added to the child table's ConstraintCollection.

The ConstraintCollection uses standard collection methods such as Add, Clear, and Remove. In addition, the Contains method can be used to look for the existence of a particular constraint in the collection.

A UniqueConstraint is created when a DataColumn with its Unique property set to true is added to a DataTable object's DataColumnCollection.

A ForeignKeyConstraint is created when a DataRelation is added to a DataSet object's DataRelationCollection.

The first example creates a DataTable, and adds a DataColumn (with its Unique property set to true) to the DataColumnCollection. The second example creates a DataSet, two DataTable objects, four columns, and a DataRelation. The count of constraints is then printed to show that a ForeignKeyConstraint and a UniqueConstraint are created when a DataRelation is added to the DataSet object's DataRelationCollection.

private void MakeTableWithUniqueConstraint()
    DataTable table = new DataTable("table");
    DataColumn column = new DataColumn("UniqueColumn");

    // Print count, name, and type.
    Console.WriteLine("Constraints.Count " 
        + table.Constraints.Count);
    Console.WriteLine(table.Constraints[0].GetType() );

    // Add a second unique column.
    column = new DataColumn("UniqueColumn2");

    // Print info again.
    Console.WriteLine("Constraints.Count " 
        + table.Constraints.Count);
    Console.WriteLine(table.Constraints[1].GetType() );

private void MakeTableWithForeignConstraint()
    // Create a DataSet.
    DataSet dataSet = new DataSet("dataSet");

    // Make two tables.
    DataTable customersTable= new DataTable("Customers");
    DataTable ordersTable = new DataTable("Orders");

    // Create four columns, two for each table.
    DataColumn name = new DataColumn("Name");
    DataColumn id = new DataColumn("ID");
    DataColumn orderId = new DataColumn("OrderID");
    DataColumn cDate = new DataColumn("OrderDate");

    // Add columns to tables.

    // Add tables to the DataSet.

    // Create a DataRelation for two of the columns.
    DataRelation myRelation = new 

    // Print TableName, Constraints.Count,  
    // ConstraintName and Type. 
    foreach(DataTable t in dataSet.Tables)
        Console.WriteLine("Constraints.Count " 
            + t.Constraints.Count);
        Console.WriteLine("ParentRelations.Count " 
            + t.ParentRelations.Count);
        Console.WriteLine("ChildRelations.Count " 
            + t.ChildRelations.Count);
        foreach(Constraint cstrnt in t.Constraints)

This type is safe for multithreaded read operations. You must synchronize any write operations.

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

Community Additions

© 2014 Microsoft