共用方式為


IDbDataAdapter.InsertCommand 屬性

定義

取得或設定一個 SQL 陳述式,用於將新紀錄插入資料來源。

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

屬性值

IDbCommand在資料Update(DataSet)集中插入新列的記錄時。

範例

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

public static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command;

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

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

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)", connection);

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

    adapter.InsertCommand = command;
    return adapter;
}
Public Shared Function CreateCustomerAdapter( _
    connection As OleDbConnection) As OleDbDataAdapter 
  
    Dim adapter As New OleDbDataAdapter()
    Dim command As OleDbCommand

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

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

    adapter.SelectCommand = command

    ' Create the InsertCommand.
    command = New OleDbCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
        "VALUES (?, ?)", connection)

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

    adapter.InsertCommand = command
    Return adapter
End Function

備註

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

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

備註

如果執行此指令回傳列數,這些列可能會根據你如何設定UpdatedRowSource物件屬性IDbCommand加入 。DataSet

適用於