ConstraintCollection.CanRemove(Constraint) Metoda

Definicja

Wskazuje, czy można usunąć element 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

Parametry

constraint
Constraint

Test Constraint do usunięcia z kolekcji.

Zwraca

true jeśli obiekt Constraint można usunąć z kolekcji; w przeciwnym razie false.

Przykłady

W poniższym przykładzie CanRemove użyto metody w celu określenia, czy można usunąć element przed próbą Constraint jego usunięcia.

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

Uwagi

Domyślne zachowanie za każdym razem, gdy DataRelation element zostanie dodany do DataSetobiektu , to dodanie elementu ForeignKeyConstraint do tabeli nadrzędnej i do UniqueConstraint tabeli podrzędnej. Element UniqueConstraint jest stosowany do kolumny klucza podstawowego tabeli nadrzędnej i ForeignKeyConstraint jest stosowany do kolumny klucza obcego tabeli podrzędnej. Ponieważ próba usunięcia elementu przed usunięciem UniqueConstraintForeignKeyConstraint powoduje zgłoszenie wyjątku CanRemove , należy zawsze użyć metody przed wywołaniem metody Remove, aby upewnić się, że UniqueConstraint można je usunąć.

Dotyczy