Constraint Class
TOC
Collapse the table of content
Expand the table of content

Constraint Class

 

Represents a constraint that can be enforced on one or more DataColumn objects.

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


public abstract class Constraint

NameDescription
System_CAPS_protmethodConstraint()

Initializes a new instance of the Constraint class.

NameDescription
System_CAPS_protproperty_DataSet

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the DataSet to which this constraint belongs.

System_CAPS_pubpropertyConstraintName

The name of a constraint in the ConstraintCollection.

System_CAPS_pubpropertyExtendedProperties

Gets the collection of user-defined constraint properties.

System_CAPS_pubpropertyTable

Gets the DataTable to which the constraint applies.

NameDescription
System_CAPS_protmethodCheckStateForProperty()

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the DataSet to which this constraint belongs.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodSetDataSet(DataSet)

Sets the constraint's DataSet.

System_CAPS_pubmethodToString()

Gets the ConstraintName, if there is one, as a string.(Overrides Object.ToString().)

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, just makes sure that all values within a particular table are unique. For more information, see DataTable Constraints.

A base Constraint constructor is not used. Primary or unique key constraints are created by using the UniqueConstraint constructor, and foreign key constraints are created by using the ForeignKeyConstraint constructor.

The following example examines the collection of constraints for a DataTable and determines whether each constraint is a UniqueConstraint or a ForeignKeyConstraint. The properties of the constraint are then displayed.

private void GetConstraints(DataTable dataTable)
{
    Console.WriteLine();

    // Print the table's name.
    Console.WriteLine("TableName: " + dataTable.TableName);

    // Iterate through the collection and 
    // print each name and type value.
    foreach(Constraint constraint in dataTable.Constraints ) 
    {
        Console.WriteLine("Constraint Name: " 
            + constraint.ConstraintName);
        Console.WriteLine("Type: " 
            + constraint.GetType().ToString());

        // If the constraint is a UniqueConstraint, 
        // print its properties using a function below.
        if(constraint is UniqueConstraint) 
        {
            PrintUniqueConstraintProperties(constraint);
        }
        // If the constraint is a ForeignKeyConstraint, 
        // print its properties using a function below.
        if(constraint is ForeignKeyConstraint) 
        {
            PrintForeigKeyConstraintProperties(constraint);
        }
    }
}

private void PrintUniqueConstraintProperties(
    Constraint constraint)
{
    UniqueConstraint uniqueConstraint;
    uniqueConstraint = (UniqueConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = uniqueConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Column Name: " 
            + columnArray[i].ColumnName);
    }
}

private void PrintForeigKeyConstraintProperties(
    Constraint constraint)
{
    ForeignKeyConstraint fkConstraint;
    fkConstraint = (ForeignKeyConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = fkConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();

    // Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns ;
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Related Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
}

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft