System.Data Namespace
Represents a constraint that can be enforced on one or more DataColumn objects.
A constraint is a rule used to maintain the integrity of data in a DataTable. ADO.NET includes two constraints:
a UniqueConstraint and a ForeignKeyConstraint.
A UniqueConstraint simply ensures that values in a table are unique, whereas a ForeignKeyConstraint determines what action to take when a value in a column that is used in one or more related tables is modified. 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.
The following example checks the collection of constraints in a DataTable and determines if the constraint is a UniqueConstraint or a ForeignKeyConstraint.
private void GetConstraints ( DataTable myTable ) {
// print the table' s name.
Response.Write ( "TableName: " + myTable.TableName );
// iterate through the collection and print each name and type value.
foreach ( Constraint cs in myTable.Constraints ) {
Response.Write ( "Constraint Name: " + cs.ConstraintName );
Response.Write ( "Type: " + cs.GetType ( ).ToString ( ) );
}
}
Private Sub GetConstraints ( myTable As DataTable )
' print the table' s name.
Response.Write ( "TableName: " + myTable.TableName )
Dim cs As Constraint
' iterate through the collection and print each name and type value.
For Each cs In myTable.Constraints
Response.Write ( "Constraint Name: " & cs.ConstraintName )
Response.Write ( "Type: " & cs.GetType ( ).ToString ( ) )
Next cs
End Sub |
|
C# |
VB |
ConstraintCollection Constraints ForeignKeyConstraint UniqueConstraint