ConstraintCollection.CanRemove(Constraint) Method

Definition

Indicates whether a Constraint can be removed.

public bool CanRemove (System.Data.Constraint constraint);

Parameters

constraint
Constraint

The Constraint to be tested for removal from the collection.

Returns

true if the Constraint can be removed from collection; otherwise, false.

Examples

The following example uses the CanRemove method to determine whether a Constraint can be removed, before trying to remove it.

private void TryRemove(DataSet dataSet)
{
    try
    {
        DataTable customersTable = dataSet.Tables["Customers"];
        Constraint constraint = customersTable.Constraints[0];
        Console.WriteLine("Can remove? " +
            customersTable.Constraints.CanRemove(constraint));
    }
    catch(Exception ex)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.",
            ex.GetType());
    }
}

Remarks

The default behavior whenever a DataRelation is added to a DataSet, is to add a ForeignKeyConstraint to the parent table and a UniqueConstraint to the child table. The UniqueConstraint is applied to the primary key column of the parent table, and the ForeignKeyConstraint is applied to the foreign key column of the child table. Because trying to remove the UniqueConstraint before removing the ForeignKeyConstraint causes an exception to be thrown, you should always use the CanRemove method before calling Remove, to make sure that the UniqueConstraint can be removed.

Applies to

Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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