Edit

Share via


Constraint Class

Definition

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

C#
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
public abstract class Constraint
C#
public abstract class Constraint
C#
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
[System.Serializable]
public abstract class Constraint
Inheritance
Constraint
Derived
Attributes

Examples

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.

C#
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)
        {
            PrintForeignKeyConstraintProperties(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 PrintForeignKeyConstraintProperties(
    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();
}

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

Constructors

Constraint()

Initializes a new instance of the Constraint class.

Properties

_DataSet

Gets the DataSet to which this constraint belongs.

ConstraintName

The name of a constraint in the ConstraintCollection.

ExtendedProperties

Gets the collection of user-defined constraint properties.

Table

Gets the DataTable to which the constraint applies.

Methods

CheckStateForProperty()

Gets the DataSet to which this constraint belongs.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SetDataSet(DataSet)

Sets the constraint's DataSet.

ToString()

Gets the ConstraintName, if there is one, as a string.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Thread Safety

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

See also