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 для удаления из коллекции.

Возвращаемое значение

Значение />, если его можно удалить из коллекции; в противном случае .

Примеры

В следующем примере метод используется 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

Комментарии

Поведение по умолчанию при каждом добавлении к ней DataRelationDataSet— добавление ForeignKeyConstraint в родительскую таблицу и UniqueConstraint дочернюю таблицу. Он UniqueConstraint применяется к столбцу первичного ключа родительской таблицы и ForeignKeyConstraint применяется к столбцу внешнего ключа дочерней таблицы. Так как попытка удалить UniqueConstraint перед удалением ForeignKeyConstraint причины возникновения исключения, следует всегда использовать CanRemove метод перед вызовом Remove, чтобы убедиться, что UniqueConstraint его можно удалить.

Применяется к