Assembly: System.Data (in system.data.dll)
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 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.
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.