共用方式為


SqlDataAdapter.InsertCommand 屬性

定義

取得或設定 Transact-SQL 語句或儲存程序,以將新紀錄插入資料來源。

public:
 property System::Data::SqlClient::SqlCommand ^ InsertCommand { System::Data::SqlClient::SqlCommand ^ get(); void set(System::Data::SqlClient::SqlCommand ^ value); };
[System.Data.DataSysDescription("DbDataAdapter_InsertCommand")]
public System.Data.SqlClient.SqlCommand InsertCommand { get; set; }
public System.Data.SqlClient.SqlCommand InsertCommand { get; set; }
[<System.Data.DataSysDescription("DbDataAdapter_InsertCommand")>]
member this.InsertCommand : System.Data.SqlClient.SqlCommand with get, set
member this.InsertCommand : System.Data.SqlClient.SqlCommand with get, set
Public Property InsertCommand As SqlCommand

屬性值

A SqlCommand 用於在 中 Update(DataSet) 插入對應於新列 DataSet的紀錄到資料庫中。

屬性

範例

以下範例建立 , SqlDataAdapter 並設定 SelectCommandInsertCommandUpdateCommandDeleteCommand 屬性。 它假設你已經建立了 SqlConnection 物件。

public static SqlDataAdapter CreateCustomerAdapter(
    SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter();

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

    // Add the parameters for the SelectCommand.
    command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
    command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

    adapter.SelectCommand = command;

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

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

    adapter.InsertCommand = command;

    // Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;

    // Create the DeleteCommand.
    command = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Add the parameters for the DeleteCommand.
    parameter = command.Parameters.Add(
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand = command;

    return adapter;
}
Public Function CreateCustomerAdapter( _
  ByVal connection As SqlConnection) As SqlDataAdapter

    Dim adapter As SqlDataAdapter = New SqlDataAdapter()

    ' Create the SelectCommand.
    Dim command As SqlCommand = New SqlCommand( _
        "SELECT * FROM Customers " & _
        "WHERE Country = @Country AND City = @City", connection)

    ' Add the parameters for the SelectCommand.
    command.Parameters.Add("@Country", SqlDbType.NVarChar, 15)
    command.Parameters.Add("@City", SqlDbType.NVarChar, 15)

    adapter.SelectCommand = command

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

    ' Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")

    adapter.InsertCommand = command

    ' Create the UpdateCommand.
    command = New SqlCommand( _
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
        "WHERE CustomerID = @oldCustomerID", connection)

    ' Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
    Dim parameter As SqlParameter = command.Parameters.Add( _
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
    parameter.SourceVersion = DataRowVersion.Original

    adapter.UpdateCommand = command

    ' Create the DeleteCommand.
    command = New SqlCommand( _
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection)

    ' Add the parameters for the DeleteCommand.
    command.Parameters.Add( _
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID")
    parameter.SourceVersion = DataRowVersion.Original

    adapter.DeleteCommand = command

    Return adapter
End Function

備註

Update期間,若未設定此屬性且主鍵資訊存在 DataSet於 , InsertCommand 則可自動生成,並設定 SelectCommand 屬性並使用 SqlCommandBuilder。 接著,任何你未設定的額外指令會由 SqlCommandBuilder產生。 此生成邏輯要求關鍵欄位資訊必須存在於 DataSet中。 如需詳細資訊,請參閱使用 CommandBuilder 產生命令

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

如果執行此指令時回傳了幾列,根據你如何設定物件的 SqlCommandUpdatedRowSource 屬性,這些列可以被加入 。DataSet

對於你傳播到資料來源Update的每一欄,都應該在 、 UpdateCommand、 或 DeleteCommand上加入一個參數。InsertCommand SourceColumn參數的屬性應該設為欄位名稱。 這表示參數的值不是手動設定的,而是從當前處理的列中特定欄位取出。

適用於

另請參閱