IDbDataAdapter.UpdateCommand 屬性

定義

取得或設定一個用於更新資料來源記錄的 SQL 語句。

public:
 property System::Data::IDbCommand ^ UpdateCommand { System::Data::IDbCommand ^ get(); void set(System::Data::IDbCommand ^ value); };
public System.Data.IDbCommand UpdateCommand { get; set; }
member this.UpdateCommand : System.Data.IDbCommand with get, set
Public Property UpdateCommand As IDbCommand

屬性值

IDbCommand 期間 Update(DataSet) 中用於更新資料來源中修改過的資料列的紀錄。

範例

以下範例建立繼承 OleDbDataAdapter 類別的實例,並設定 SelectCommandUpdateCommand 屬性。 它假設你已經建立了 OleDbConnection 一個物件。

private static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
{
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
    OleDbCommand command;
    OleDbParameter parameter;

    // Create the SelectCommand.
    command = new OleDbCommand("SELECT * FROM dbo.Customers " +
        "WHERE Country = ? AND City = ?", connection);

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

    dataAdapter.SelectCommand = command;

    // Create the UpdateCommand.
    command = new OleDbCommand(
        "UPDATE dbo.Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?", connection);

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

    parameter = command.Parameters.Add(
        "oldCustomerID", OleDbType.Char, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    dataAdapter.UpdateCommand = command;

    return dataAdapter;
}
Private Function CreateCustomerAdapter( _
    ByVal connection As OleDbConnection) As OleDbDataAdapter

    Dim dataAdapter As New OleDbDataAdapter()
    Dim command As OleDbCommand
    Dim parameter As OleDbParameter

    ' Create the SelectCommand.
    command = New OleDbCommand("SELECT * FROM dbo.Customers " & _
        "WHERE Country = ? AND City = ?", connection)

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

    dataAdapter.SelectCommand = command

    ' Create the UpdateCommand.
    command = New OleDbCommand("UPDATE dbo.Customers " & _
        "SET CustomerID = ?, CompanyName = ? " & _
        "WHERE CustomerID = ?", connection)

    command.Parameters.Add( _
        "CustomerID", OleDbType.Char, 5, "CustomerID")
    command.Parameters.Add( _
        "CompanyName", OleDbType.VarChar, 40, "CompanyName")

    parameter = command.Parameters.Add( _
        "oldCustomerID", OleDbType.Char, 5, "CustomerID")
    parameter.SourceVersion = DataRowVersion.Original

    dataAdapter.UpdateCommand = command

    Return dataAdapter
End Function

備註

Update期間,若未設定此屬性且主鍵資訊存在 DataSet於 , UpdateCommand 且 可自動產生,且 若設定 SelectCommand .NET Framework 資料提供者的屬性。 接著,任何你未設定的額外指令會由 CommandBuilder 產生。 此生成邏輯要求關鍵欄位資訊必須存在於 DataSet中。 更多資訊請參閱 使用 CommandBuilders 產生指令

UpdateCommand 被指派給先前建立 IDbCommand的 時,則 IDbCommand 不會被複製。 它 UpdateCommand 會維持對先前建立 IDbCommand 物件的參考。

Note

若執行此指令回傳列,這些列會被 DataSet加入 。

適用於