OdbcDataAdapter.DeleteCommand Propriedade

Definição

Obtém ou define uma instrução SQL ou um procedimento armazenado usado para excluir registros na fonte de dados.

public:
 property System::Data::Odbc::OdbcCommand ^ DeleteCommand { System::Data::Odbc::OdbcCommand ^ get(); void set(System::Data::Odbc::OdbcCommand ^ value); };
public System.Data.Odbc.OdbcCommand DeleteCommand { get; set; }
public System.Data.Odbc.OdbcCommand? DeleteCommand { get; set; }
member this.DeleteCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property DeleteCommand As OdbcCommand

Valor da propriedade

Um OdbcCommand usado durante uma operação de atualização para excluir registros na fonte de dados que correspondem a linhas excluídas no DataSet.

Exemplos

O exemplo a seguir cria um OdbcDataAdapter e define as SelectCommand propriedades e DeleteCommand . Ele pressupõe que você já tenha criado um OdbcConnection objeto .

public static OdbcDataAdapter CreateDataAdapter(
    OdbcConnection connection)
{
    string selectCommand =
        "SELECT CustomerID, CompanyName FROM Customers";

    OdbcDataAdapter adapter = new OdbcDataAdapter(
        selectCommand, connection);
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the Insert, Update and Delete commands.
    adapter.InsertCommand = new OdbcCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)");

    adapter.UpdateCommand = new OdbcCommand(
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?");

    adapter.DeleteCommand = new OdbcCommand(
        "DELETE FROM Customers WHERE CustomerID = ?");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}
Public Function CreateDataAdapter( _
    ByVal connection As OdbcConnection) As OdbcDataAdapter

    Dim selectCommand As String = _
        "SELECT CustomerID, CompanyName FROM Customers"
    Dim adapter As OdbcDataAdapter = _
        New OdbcDataAdapter(selectCommand, connection)

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the Insert, Update and Delete commands.
    adapter.InsertCommand = New OdbcCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (?, ?)")

    adapter.UpdateCommand = New OdbcCommand( _
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
        "WHERE CustomerID = ?")

    adapter.DeleteCommand = New OdbcCommand( _
        "DELETE FROM Customers WHERE CustomerID = ?")

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add( _
        "@CompanyName", OdbcType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add( _
        "@CompanyName", OdbcType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add( _
        "@oldCustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    Return adapter
End Function

Comentários

Quando a DeleteCommand propriedade é atribuída a um criado OdbcCommandanteriormente, o OdbcCommand não é clonado. Em vez disso, o DeleteCommand mantém uma referência ao criado OdbcCommandanteriormente.

Durante uma operação de atualização, se DeleteCommand não estiver definido e as informações de chave primária estiverem presentes no DataSet, você poderá usar a OdbcCommandBuilder classe para gerar automaticamente o DeleteCommande comandos adicionais necessários para reconciliar o DataSet com a fonte de dados. Para fazer isso, defina a SelectCommand propriedade do OdbcDataAdapter. A lógica de geração também exige que as informações de coluna de chave estejam presentes no DataSet. Para obter mais informações, confira Gerar comandos com CommandBuilders.

Aplica-se a

Confira também