This documentation is archived and is not being maintained.

OdbcDataAdapter.UpdateCommand Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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

[Visual Basic]
Public Shadows Property UpdateCommand As OdbcCommand
[C#]
public new OdbcCommand UpdateCommand {get; set;}
[C++]
public: __property OdbcCommand* get_UpdateCommand();
public: __property void set_UpdateCommand(OdbcCommand*);
[JScript]
public hide function get UpdateCommand() : OdbcCommand;
public function set UpdateCommand(OdbcCommand);

Property Value

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

Remarks

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 Automatically Generated Commands.

Note   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.

Example

[Visual Basic, C#, C++] The following example creates an OdbcDataAdapter and sets the SelectCommand and UpdateCommand properties. It assumes you have already created an OdbcConnection object.

[Visual Basic] 
Public Shared Function CreateCustomerAdapter(conn As OdbcConnection) As OdbcDataAdapter 
  
  Dim da As OdbcDataAdapter = New OdbcDataAdapter()
  Dim cmd As OdbcCommand
  Dim parm As OdbcParameter

  ' Create the SelectCommand.

  cmd = New OdbcCommand("SELECT * FROM Customers " & _
                       "WHERE Country = ? AND City = ?", conn)

  cmd.Parameters.Add("@Country", OdbcType.NVarChar, 15)
  cmd.Parameters.Add("@City", OdbcType.NVarChar, 15)

  da.SelectCommand = cmd

  ' Create the UpdateCommand.

  cmd = New OdbcCommand("UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
                       "WHERE CustomerID = ?", conn)

  cmd.Parameters.Add("@CustomerID", OdbcType.NChar, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", OdbcType.NVarChar, 40, "CompanyName")

  parm = cmd.Parameters.Add("@oldCustomerID", OdbcType.NChar, 5, "CustomerID")
  parm.SourceVersion = DataRowVersion.Original

  da.UpdateCommand = cmd

  Return da
End Function

[C#] 
public static OdbcDataAdapter CreateCustomerAdapter(OdbcConnection conn)
{
  OdbcDataAdapter da = new OdbcDataAdapter();
  OdbcCommand cmd;
  OdbcParameter parm;

  // Create the SelectCommand.

  cmd = new OdbcCommand("SELECT * FROM Customers " +
                       "WHERE Country = ? AND City = ?", conn);

  cmd.Parameters.Add("@Country", OdbcType.NVarChar, 15);
  cmd.Parameters.Add("@City", OdbcType.NVarChar, 15);

  da.SelectCommand = cmd;

  // Create the UpdateCommand.

  cmd = new OdbcCommand("UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
                       "WHERE CustomerID = ?", conn);

  cmd.Parameters.Add("@CustomerID", OdbcType.NChar, 5, "CustomerID");
  cmd.Parameters.Add("@CompanyName", OdbcType.NVarChar, 40, "CompanyName");

  parm = cmd.Parameters.Add("@oldCustomerID", OdbcType.NChar, 5, "CustomerID");
  parm.SourceVersion = DataRowVersion.Original;

  da.UpdateCommand = cmd;

  return da;
}

[C++] 
public:
    static OdbcDataAdapter* CreateCustomerAdapter(OdbcConnection* conn)
    {
        OdbcDataAdapter* da = new OdbcDataAdapter();
        OdbcCommand* cmd;
        OdbcParameter* parm;

        // Create the SelectCommand.
        cmd = new OdbcCommand(S"SELECT * FROM Customers WHERE Country = ? AND City = ?", conn);

        cmd->Parameters->Add(S"@Country", OdbcType::NVarChar, 15);
        cmd->Parameters->Add(S"@City", OdbcType::NVarChar, 15);

        da->SelectCommand = cmd;

        // Create the UpdateCommand.
        cmd = new OdbcCommand(S"UPDATE Customers SET CustomerID = ?, CompanyName = ? WHERE CustomerID = ?", conn);

        cmd->Parameters->Add(S"@CustomerID", OdbcType::NChar, 5, S"CustomerID");
        cmd->Parameters->Add(S"@CompanyName", OdbcType::NVarChar, 40, S"CompanyName");

        parm = cmd->Parameters->Add(S"@oldCustomerID", OdbcType::NChar, 5, S"CustomerID");
        parm->SourceVersion = DataRowVersion::Original;

        da->UpdateCommand = cmd;

        return da;
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OdbcDataAdapter Class | OdbcDataAdapter Members | System.Data.Odbc Namespace | DeleteCommand | InsertCommand | SelectCommand

Show: