Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

OleDbDataAdapter.UpdateCommand Property

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

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

Property Value

An OleDbCommand used during Update to update records in the data source that correspond to modified rows in the DataSet.

Remarks

During Update, if this property is not set and primary key information is present in the DataSet, the UpdateCommand can be generated automatically if you set the SelectCommand property and use the OleDbCommandBuilder. Then, any additional commands that you do not set are generated by the OleDbCommandBuilder. This generation logic requires key column information to be present in the DataSet. For more information see Automatically Generated Commands.

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

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

Example

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

[Visual Basic] 
Public Shared Function CreateCustomerAdapter(conn As OleDbConnection) As OleDbDataAdapter 
  
  Dim da As OleDbDataAdapter = New OleDbDataAdapter()
  Dim cmd As OleDbCommand
  Dim parm As OleDbParameter

  ' Create the SelectCommand.

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

  cmd.Parameters.Add("@Country", OleDbType.VarChar, 15)
  cmd.Parameters.Add("@City", OleDbType.VarChar, 15)

  da.SelectCommand = cmd

  ' Create the UpdateCommand.

  cmd = New OleDbCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
                       "WHERE CustomerID = @oldCustomerID", conn)

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID")
  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName")

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

  da.UpdateCommand = cmd

  Return da
End Function

[C#] 
public static OleDbDataAdapter CreateCustomerAdapter(OleDbConnection conn)
{
  OleDbDataAdapter da = new OleDbDataAdapter();
  OleDbCommand cmd;
  OleDbParameter parm;

  // Create the SelectCommand.

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

  cmd.Parameters.Add("@Country", OleDbType.VarChar, 15);
  cmd.Parameters.Add("@City", OleDbType.VarChar, 15);

  da.SelectCommand = cmd;

  // Create the UpdateCommand.

  cmd = new OleDbCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
                       "WHERE CustomerID = @oldCustomerID", conn);

  cmd.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID");
  cmd.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName");

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

  da.UpdateCommand = cmd;

  return da;
}

[C++] 
public:
static OleDbDataAdapter* CreateCustomerAdapter(OleDbConnection* conn)
{
  OleDbDataAdapter* da = new OleDbDataAdapter();
  OleDbCommand* cmd;
  OleDbParameter* parm;

  // Create the SelectCommand.

  cmd = new OleDbCommand(S"SELECT * FROM Customers " 
                         S"WHERE Country = @Country AND City = @City", conn);

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

  da->SelectCommand = cmd;

  // Create the UpdateCommand.

  cmd = new OleDbCommand(S"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " 
                         S"WHERE CustomerID = @oldCustomerID", conn);

  cmd->Parameters->Add(S"@CustomerID", OleDbType::Char, 5, S"CustomerID");
  cmd->Parameters->Add(S"@CompanyName", OleDbType::VarChar, 40, S"CompanyName");

  parm = cmd->Parameters->Add(S"@oldCustomerID", OleDbType::Char, 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

OleDbDataAdapter Class | OleDbDataAdapter Members | System.Data.OleDb Namespace | DeleteCommand | InsertCommand | SelectCommand

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.