ConstraintCollection.CanRemove(Constraint) 方法

定义

指示是否可移除 Constraint

public:
 bool CanRemove(System::Data::Constraint ^ constraint);
public bool CanRemove (System.Data.Constraint constraint);
member this.CanRemove : System.Data.Constraint -> bool
Public Function CanRemove (constraint As Constraint) As Boolean

参数

constraint
Constraint

要对其进行测试以查看是否可从集合删除的 Constraint

返回

如果可从集合中移除 Constraint,则为 true;否则为 false

示例

以下示例在尝试删除之前使用 CanRemove 方法确定是否 Constraint 可以删除 。

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());
    }
}
Private Sub TryRemove(dataSet As DataSet)
    Try
        Dim customersTable As DataTable = dataSet.Tables("Customers")
        Dim constraint As Constraint = customersTable.Constraints(0)
        Console.WriteLine("Can remove? " & _
            customersTable.Constraints.CanRemove(constraint).ToString())

    Catch ex As Exception
        ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
            ex.GetType().ToString())
    End Try
End Sub

注解

每当 DataRelation 将 添加到 时 DataSet,默认行为是将 添加到 ForeignKeyConstraint 父表,将 添加到 UniqueConstraint 子表。 UniqueConstraint应用于父表的主键列,将 ForeignKeyConstraint 应用于子表的外键列。 由于尝试在删除 之前删除 UniqueConstraint 会导致引发异常,因此在调用 Remove 之前应始终使用 CanRemove 方法,以确保UniqueConstraint可以ForeignKeyConstraint删除 。

适用于