共用方式為


OdbcDataAdapter.InsertCommand 屬性

定義

取得或設定 SQL 陳述式或預存程序,用來插入新記錄到資料來源。

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

屬性值

更新作業期間使用的 OdbcCommand,可將資料來源中對應新資料列的記錄插入 DataSet

範例

下列範例會 OdbcDataAdapter 建立 並設定 SelectCommandInsertCommand 屬性。 它假設您已經建立 OdbcConnection 物件。

public static OdbcDataAdapter CreateDataAdapter(
    OdbcConnection connection)
{
    string selectCommand =
        "SELECT CustomerID, CompanyName FROM Customers";

    OdbcDataAdapter adapter = new OdbcDataAdapter(
        selectCommand, connection);
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the Insert, Update and Delete commands.
    adapter.InsertCommand = new OdbcCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)");

    adapter.UpdateCommand = new OdbcCommand(
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?");

    adapter.DeleteCommand = new OdbcCommand(
        "DELETE FROM Customers WHERE CustomerID = ?");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        OdbcType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        OdbcType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}
Public Function CreateDataAdapter( _
    ByVal connection As OdbcConnection) As OdbcDataAdapter

    Dim selectCommand As String = _
        "SELECT CustomerID, CompanyName FROM Customers"
    Dim adapter As OdbcDataAdapter = _
        New OdbcDataAdapter(selectCommand, connection)

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the Insert, Update and Delete commands.
    adapter.InsertCommand = New OdbcCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (?, ?)")

    adapter.UpdateCommand = New OdbcCommand( _
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
        "WHERE CustomerID = ?")

    adapter.DeleteCommand = New OdbcCommand( _
        "DELETE FROM Customers WHERE CustomerID = ?")

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add( _
        "@CompanyName", OdbcType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add( _
        "@CompanyName", OdbcType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add( _
        "@oldCustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add( _
        "@CustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    Return adapter
End Function

備註

InsertCommand將 屬性指派給先前建立OdbcCommand的物件時,OdbcCommand不會複製 。 相反地, InsertCommand 維護先前建立 OdbcCommand之的參考。

在更新作業期間,如果未 InsertCommand 設定 ,且主 DataSet鍵資訊存在於 中,您可以使用 OdbcCommandBuilder 類別來自動產生 InsertCommand,以及協調 數據源所需的 DataSet 其他命令。 若要這樣做,請設定 SelectCommandOdbcDataAdapter屬性。 產生邏輯也需要在 中 DataSet出現索引鍵數據行資訊。 如需詳細資訊,請參閱使用 CommandBuilder 產生命令

注意

如果執行此指令會傳回資料列,則這些資料列可能會根據您設定 UpdatedRowSource 物件的 屬性OdbcCommand而新增至 DataSet

適用於

另請參閱