共用方式為


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

屬性值

SqlCommand 期間使用的 Update(DataSet),用來將資料錄插入至資料庫,其對應到 DataSet 中的新資料列。

屬性

範例

下列範例會SqlDataAdapter建立 ,並設定SelectCommandInsertCommandUpdateCommand、 和 DeleteCommand 屬性。 假設您已經建立 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中,則如果您設定 SelectCommand 屬性並使用 SqlCommandBuilderInsertCommand則可以自動產生 。 然後,您未設定的任何其他命令都會由 SqlCommandBuilder產生。 此產生邏輯需要在 中 DataSet提供索引鍵數據行資訊。 如需詳細資訊,請參閱使用 CommandBuilder 產生命令

當 指定先前建立SqlCommand的時InsertCommandSqlCommand不會複製 。 會 InsertCommand 維護先前建立 SqlCommand 對象的參考。

如果執行此指令傳回資料列,則可以根據您設定物件的 UpdatedRowSource 屬性SqlCommand的方式,將這些資料列加入 DataSet 至 。

對於您傳播至 上 Update資料來源的每個資料行,應該將參數新增至 InsertCommandUpdateCommandDeleteCommand。 參數 SourceColumn 的 屬性應該設定為數據行的名稱。 這表示參數的值不是手動設定,而是取自目前處理之數據列中的特定數據行。

適用於

另請參閱