Constraint Class
Represents a constraint that can be enforced on one or more DataColumn objects.
For a list of all members of this type, see Constraint Members.
System.Object
System.Data.Constraint
System.Data.ForeignKeyConstraint
System.Data.UniqueConstraint
[Visual Basic] <Serializable> MustInherit Public Class Constraint [C#] [Serializable] public abstract class Constraint [C++] [Serializable] public __gc __abstract class Constraint [JScript] public Serializable abstract class Constraint
Thread Safety
This type is safe for multithreaded read operations. You must synchronize any write operations.
Remarks
A constraint is a rule used to maintain the integrity of the data in the DataTable. For example, when you delete a value that is used in one or more related tables, a ForeignKeyConstraint determines whether the values in the related tables are also deleted, set to null values, set to default values, or whether no action occurs. A UniqueConstraint, on the other hand, simply ensures that all values within a particular table are unique. For more information, see Adding Constraints to a Table.
A base Constraint constructor is not used. Primary or unique key constraints are created using the UniqueConstraint constructor, and foreign key constraints are created using the ForeignKeyConstraint constructor.
Example
[Visual Basic, C#, C++] The following example checks the collection of constraints for a DataTable and determines if each constraint is a UniqueConstraint or a ForeignKeyConstraint. The properties of the constraint are then displayed.
[Visual Basic] Private Sub GetConstraints(myTable As DataTable) Console.WriteLine() ' Print the table's name. Console.WriteLine("TableName: " + myTable.TableName) ' Iterate through the collection and print each name and type value. Dim cs As Constraint For Each cs In myTable.Constraints Console.WriteLine("Constraint Name: " + cs.ConstraintName) Console.WriteLine("Type: " + cs.GetType().ToString()) ' If the constraint is a UniqueConstraint, print its properties ' using a function below. If TypeOf cs Is UniqueConstraint Then PrintUniqueConstraintProperties(cs) End If ' If the constraint is a ForeignKeyConstraint, print its properties ' using a function below. If TypeOf cs Is ForeignKeyConstraint Then PrintForeigKeyConstraintProperties(cs) End If Next cs End Sub Private Sub PrintUniqueConstraintProperties(cs As Constraint) Dim uCS As UniqueConstraint uCS = CType(cs, UniqueConstraint) ' Get the Columns as an array. Dim colArray() As DataColumn colArray = uCS.Columns ' Print each column's name. Dim i As Integer For i = 0 To colArray.Length - 1 Console.WriteLine("Column Name: " + colArray(i).ColumnName) Next i End Sub Private Sub PrintForeigKeyConstraintProperties(cs As Constraint) Dim fkCS As ForeignKeyConstraint fkCS = CType(cs, ForeignKeyConstraint) ' Get the Columns as an array. Dim colArray() As DataColumn colArray = fkCS.Columns ' Print each column's name. Dim i As Integer For i = 0 To colArray.Length - 1 Console.WriteLine("Column Name: " + colArray(i).ColumnName) Next i Console.WriteLine() ' Get the related columns and print each columns name. colArray = fkCS.RelatedColumns For i = 0 To colArray.Length - 1 Console.WriteLine("Related Column Name: " + colArray(i).ColumnName) Next i Console.WriteLine() End Sub [C#] private void GetConstraints(DataTable myTable){ Console.WriteLine(); // Print the table's name. Console.WriteLine("TableName: " + myTable.TableName); // Iterate through the collection and print each name and type value. foreach(Constraint cs in myTable.Constraints ) { Console.WriteLine("Constraint Name: " + cs.ConstraintName); Console.WriteLine("Type: " + cs.GetType().ToString()); // If the constraint is a UniqueConstraint, print its properties using // a function below. if(cs is UniqueConstraint) { PrintUniqueConstraintProperties(cs); } // If the constraint is a ForeignKeyConstraint, print its properties // using a function below. if(cs is ForeignKeyConstraint) { PrintForeigKeyConstraintProperties(cs); } } } private void PrintUniqueConstraintProperties(Constraint cs){ UniqueConstraint uCS; uCS = (UniqueConstraint) cs; // Get the Columns as an array. DataColumn[] colArray; colArray = uCS.Columns; // Print each column's name. for(int i = 0;i<colArray.Length ;i++) { Console.WriteLine("Column Name: " + colArray[i].ColumnName); } } private void PrintForeigKeyConstraintProperties(Constraint cs){ ForeignKeyConstraint fkCS; fkCS = (ForeignKeyConstraint) cs; // Get the Columns as an array. DataColumn[] colArray; colArray = fkCS.Columns; // Print each column's name. for(int i = 0;i<colArray.Length ;i++) { Console.WriteLine("Column Name: " + colArray[i].ColumnName); } Console.WriteLine(); // Get the related columns and print each columns name. colArray = fkCS.RelatedColumns ; for(int i = 0;i<colArray.Length ;i++) { Console.WriteLine("Related Column Name: " + colArray[i].ColumnName); } Console.WriteLine(); } [C++] private: void GetConstraints(DataTable* myTable){ Console::WriteLine(); // Print the table's name. Console::WriteLine(S"TableName: {0}", myTable->TableName); // Iterate through the collection and print each name and type value. System::Collections::IEnumerator* myEnum = myTable->Constraints->GetEnumerator(); while (myEnum->MoveNext()) { Constraint* cs = __try_cast<Constraint*>(myEnum->Current); Console::WriteLine(S"Constraint Name: {0}", cs->ConstraintName); Console::WriteLine(S"Type: {0}", cs->GetType()); // If the constraint is a UniqueConstraint, print its properties using // a function below. if(dynamic_cast<UniqueConstraint*>(cs)) { PrintUniqueConstraintProperties(cs); } // If the constraint is a ForeignKeyConstraint, print its properties // using a function below. if(dynamic_cast<ForeignKeyConstraint*>(cs)) { PrintForeigKeyConstraintProperties(cs); } } } void PrintUniqueConstraintProperties(Constraint* cs){ UniqueConstraint* uCS; uCS = dynamic_cast<UniqueConstraint*> (cs); // Get the Columns as an array. DataColumn* colArray[]; colArray = uCS->Columns; // Print each column's name. for(int i = 0;i<colArray->Length ;i++) { Console::WriteLine(S"Column Name: {0}", colArray[i]->ColumnName); } } void PrintForeigKeyConstraintProperties(Constraint* cs){ ForeignKeyConstraint* fkCS; fkCS = dynamic_cast<ForeignKeyConstraint*> (cs); // Get the Columns as an array. DataColumn* colArray[]; colArray = fkCS->Columns; // Print each column's name. for(int i = 0;i<colArray->Length ;i++) { Console::WriteLine(S"Column Name: {0}", colArray[i]->ColumnName); } Console::WriteLine(); // Get the related columns and print each columns name. colArray = fkCS->RelatedColumns ; for(int i = 0;i<colArray->Length ;i++) { Console::WriteLine(S"Related Column Name: {0}", colArray[i]->ColumnName); } Console::WriteLine(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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
Constraint Members | System.Data Namespace | ConstraintCollection | Constraints | ForeignKeyConstraint | UniqueConstraint