共用方式為


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 會導致 ForeignKeyConstraint 擲回例外狀況,所以您應該一律使用 CanRemove 方法,再呼叫 Remove,以確保 UniqueConstraint 可以移除 。

適用於