Export (0) Print
Expand All

ConstraintCollection Class

Represents a collection of constraints for a DataTable.

For a list of all members of this type, see ConstraintCollection Members.

System.Object
   System.Data.InternalDataCollectionBase
      System.Data.ConstraintCollection

[Visual Basic]
<Serializable>
Public Class ConstraintCollection
   Inherits InternalDataCollectionBase
[C#]
[Serializable]
public class ConstraintCollection : InternalDataCollectionBase
[C++]
[Serializable]
public __gc class ConstraintCollection : public
   InternalDataCollectionBase
[JScript]
public
   Serializable
class ConstraintCollection extends
   InternalDataCollectionBase

Thread Safety

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

Remarks

The ConstraintCollection is accessed through the DataTable.Constraints property.

The ConstraintCollection can contain both UniqueConstraint and ForeignKeyConstraint objects for the DataTable. A UniqueConstraint object ensures that data in a given column is always unique to preserve the data integrity. The ForeignKeyConstraint determines what will happen 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.

Note   When you add a DataRelation, which 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 check 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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Private Sub MakeTableWithUniqueConstraint()
    Dim myTable As New DataTable("myTable")
    Dim myColumn As New DataColumn("UniqueColumn")
    myColumn.Unique = True
    myTable.Columns.Add(myColumn)
    ' Print count, name, and type.
    Console.WriteLine("Constraints.Count " _
       + myTable.Constraints.Count.ToString())
    Console.WriteLine(myTable.Constraints(0).ConstraintName)
    Console.WriteLine(myTable.Constraints(0).GetType().ToString())
    ' Add a second unique column.
    myColumn = New DataColumn("UniqueColumn2")
    myColumn.Unique = True
    myTable.Columns.Add(myColumn)
    ' Print info again.
    Console.WriteLine("Constraints.Count " _
       + myTable.Constraints.Count.ToString())
    Console.WriteLine(myTable.Constraints(1).ConstraintName)
    Console.WriteLine(myTable.Constraints(1).GetType().ToString())
End Sub

Private Sub MakeTableWithForeignConstraint()
    ' Create a DataSet.
    Dim myDataSet As New DataSet("myDataSet")
    ' Make two tables.
    Dim tCust As New DataTable("Customers")
    Dim tOrders As New DataTable("Orders")
    ' Create four columns, two for each table.
    Dim cName As New DataColumn("Name")
    Dim cId As New DataColumn("ID")
    Dim cOrderID As New DataColumn("OrderID")
    Dim [cDate] As New DataColumn("OrderDate")
    
    ' Add columns to tables.
    tCust.Columns.Add(cName)
    tCust.Columns.Add(cId)
    tOrders.Columns.Add(cOrderID)
    tOrders.Columns.Add([cDate])
    
    ' Add tables to the DataSet.
    myDataSet.Tables.Add(tCust)
    myDataSet.Tables.Add(tOrders)
    ' Create a DataRelation for two of the columns.
    Dim myRelation As New DataRelation _
       ("CustomersOrders", cId, cOrderID, True)
    myDataSet.Relations.Add(myRelation)
    ' Print TableName, Constraints.Count, ConstraintName and Type.
    Dim t As DataTable
    For Each t In  myDataSet.Tables
        Console.WriteLine(t.TableName)
        Console.WriteLine("Constraints.Count " _
           + t.Constraints.Count.ToString())
        Console.WriteLine("ParentRelations.Count " _
           + t.ParentRelations.Count.ToString())
        Console.WriteLine("ChildRelations.Count " _
           + t.ChildRelations.Count.ToString())
        Dim cstrnt As Constraint
        For Each cstrnt In  t.Constraints
            Console.WriteLine(cstrnt.ConstraintName)
            Console.WriteLine(cstrnt.GetType())
        Next cstrnt
    Next t
End Sub

[C#] 
private void MakeTableWithUniqueConstraint(){
    DataTable myTable = new DataTable("myTable");
    DataColumn myColumn = new DataColumn("UniqueColumn");
    myColumn.Unique=true;
    myTable.Columns.Add(myColumn);
    // Print count, name, and type.
    Console.WriteLine("Constraints.Count " + myTable.Constraints.Count);
    Console.WriteLine(myTable.Constraints[0].ConstraintName);
    Console.WriteLine(myTable.Constraints[0].GetType() );
    // Add a second unique column.
    myColumn = new DataColumn("UniqueColumn2");
    myColumn.Unique=true;
    myTable.Columns.Add(myColumn);
    // Print info again.
    Console.WriteLine("Constraints.Count " + myTable.Constraints.Count);
    Console.WriteLine(myTable.Constraints[1].ConstraintName);
    Console.WriteLine(myTable.Constraints[1].GetType() );
 }
     
 private void MakeTableWithForeignConstraint(){
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Make two tables.
    DataTable tCust= new DataTable("Customers");
    DataTable tOrders = new DataTable("Orders");
    // Create four columns, two for each table.
    DataColumn cName = new DataColumn("Name");
    DataColumn cId = new DataColumn("ID");
    DataColumn cOrderID = new DataColumn("OrderID");
    DataColumn cDate = new DataColumn("OrderDate");
 
    // Add columns to tables.
    tCust.Columns.Add(cName);
    tCust.Columns.Add(cId);
    tOrders.Columns.Add(cOrderID);
    tOrders.Columns.Add(cDate);
 
    // Add tables to the DataSet.
    myDataSet.Tables.Add(tCust);
    myDataSet.Tables.Add(tOrders); 
    // Create a DataRelation for two of the columns.
    DataRelation myRelation = new 
       DataRelation("CustomersOrders",cId,cOrderID,true);
    myDataSet.Relations.Add(myRelation);
    // Print TableName, Constraints.Count, ConstraintName and Type.
    foreach(DataTable t in myDataSet.Tables){
       Console.WriteLine(t.TableName);
       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){
          Console.WriteLine(cstrnt.ConstraintName);
          Console.WriteLine(cstrnt.GetType());
       }
    }
 }

[C++] 
private:
void MakeTableWithUniqueConstraint(){
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* myColumn = new DataColumn(S"UniqueColumn");
    myColumn->Unique=true;
    myTable->Columns->Add(myColumn);
    // Print count, name, and type.
    Console::WriteLine(S"Constraints->Count {0}", __box(myTable->Constraints->Count));
    Console::WriteLine(myTable->Constraints->Item[0]->ConstraintName);
    Console::WriteLine(myTable->Constraints->Item[0]->GetType() );
    // Add a second unique column.
    myColumn = new DataColumn(S"UniqueColumn2");
    myColumn->Unique=true;
    myTable->Columns->Add(myColumn);
    // Print info again.
    Console::WriteLine(S"Constraints->Count {0}", __box(myTable->Constraints->Count));
    Console::WriteLine(myTable->Constraints->Item[1]->ConstraintName);
    Console::WriteLine(myTable->Constraints->Item[1]->GetType() );
 }
     
private:
 void MakeTableWithForeignConstraint(){
    // Create a DataSet.
    DataSet* myDataSet = new DataSet(S"myDataSet");
    // Make two tables.
    DataTable* tCust= new DataTable(S"Customers");
    DataTable* tOrders = new DataTable(S"Orders");
    // Create four columns, two for each table.
    DataColumn* cName = new DataColumn(S"Name");
    DataColumn* cId = new DataColumn(S"ID");
    DataColumn* cOrderID = new DataColumn(S"OrderID");
    DataColumn* cDate = new DataColumn(S"OrderDate");
 
    // Add columns to tables.
    tCust->Columns->Add(cName);
    tCust->Columns->Add(cId);
    tOrders->Columns->Add(cOrderID);
    tOrders->Columns->Add(cDate);
 
    // Add tables to the DataSet.
    myDataSet->Tables->Add(tCust);
    myDataSet->Tables->Add(tOrders); 
    // Create a DataRelation for two of the columns.
    DataRelation* myRelation = new 
       DataRelation(S"CustomersOrders",cId,cOrderID,true);
    myDataSet->Relations->Add(myRelation);
    // Print TableName, Constraints.Count, ConstraintName and Type.
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* t = __try_cast<DataTable*>(myEnum->Current);
       Console::WriteLine(t->TableName);
       Console::WriteLine(S"Constraints->Count {0}", __box(t->Constraints->Count));
       Console::WriteLine(S"ParentRelations->Count {0}", __box(t->ParentRelations->Count));
       Console::WriteLine(S"ChildRelations->Count {0}", __box(t->ChildRelations->Count));
       System::Collections::IEnumerator* myEnum1 = t->Constraints->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          Constraint* cstrnt = __try_cast<Constraint*>(myEnum1->Current);
          Console::WriteLine(cstrnt->ConstraintName);
          Console::WriteLine(cstrnt->GetType());
       }
    }
 }

[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.

Requirements

Namespace: System.Data

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

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

See Also

ConstraintCollection Members | System.Data Namespace | ForeignKeyConstraint | UniqueConstraint | DataRelation

Show:
© 2014 Microsoft