OdbcDataAdapter.UpdateCommand Property

 

Gets or sets an SQL statement or stored procedure used to update records in the data source.

Namespace:   System.Data.Odbc
Assembly:  System.Data (in System.Data.dll)

public OdbcCommand UpdateCommand { get; set; }

Property Value

Type: System.Data.Odbc.OdbcCommand

An OdbcCommand used during an update operation to update records in the data source that correspond to modified rows in the DataSet.

When UpdateCommand is assigned to a previously created OdbcCommand, the OdbcCommand is not cloned. Instead, the UpdateCommand maintains a reference to the previously created OdbcCommand object.

During an update operation, if UpdateCommand is not set and primary key information is present in the DataSet, you can use the OdbcCommandBuilder class to automatically generate UpdateCommand, and additional commands needed to reconcile the DataSet to the data source. To do this, set the SelectCommand property of the OdbcDataAdapter. The generation logic also requires key column information to be present in the DataSet. For more information, see Generating Commands with CommandBuilders.

System_CAPS_noteNote

If execution of this command returns rows, these rows may be merged with the DataSet depending upon how you set the UpdatedRowSource property of the OdbcCommand object.

The following example creates an OdbcDataAdapter and sets the SelectCommand and UpdateCommand properties. It assumes that you have already created an OdbcConnection object.

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;
}

.NET Framework
Available since 1.1
Return to top
Show: