OdbcDataAdapter.InsertCommand 属性

定义

获取或设置 SQL 语句或用于将新记录插入数据源的存储过程。Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

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; }
member this.InsertCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property InsertCommand As OdbcCommand

属性值

在更新操作期间用于在对应于 OdbcCommand 中新行的数据源中插入记录的 DataSetAn OdbcCommand used during an update operation to insert records in the data source that correspond to new rows in the DataSet.

示例

下面的示例创建一个 OdbcDataAdapter,并设置 SelectCommandInsertCommand 属性。The following example creates an OdbcDataAdapter and sets the SelectCommand and InsertCommand properties. 假设您已经创建了一个 OdbcConnection 对象。It assumes that you have already created an OdbcConnection object.

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 对象时,不会克隆 OdbcCommandWhen the InsertCommand property is assigned to a previously created OdbcCommand object, the OdbcCommand is not cloned. 相反,InsertCommand 维护对先前创建的 OdbcCommand的引用。Instead, InsertCommand maintains a reference to the previously created OdbcCommand.

在更新操作过程中,如果未设置 InsertCommand 并且主键信息存在于 DataSet中,则可以使用 OdbcCommandBuilder 类来自动生成 InsertCommand,并使用将 DataSet 与数据源进行协调所需的其他命令。During an update operation, if InsertCommand is not set and primary key information is present in the DataSet, you can use the OdbcCommandBuilder class to automatically generate InsertCommand, and additional commands needed to reconcile the DataSet to the data source. 为此,请设置 OdbcDataAdapterSelectCommand 属性。To do this, set the SelectCommand property of the OdbcDataAdapter. 生成逻辑还要求密钥列信息存在于 DataSet中。The generation logic also requires key column information to be present in the DataSet. 有关详细信息,请参阅通过 Commandbuilder 生成命令For more information, see Generating Commands with CommandBuilders.

备注

如果执行此命令将返回行,则可能会将这些行添加到 DataSet,具体取决于如何设置 OdbcCommand 对象的 UpdatedRowSource 属性。If execution of this command returns rows, these rows may be added to the DataSet depending upon how you set the UpdatedRowSource property of the OdbcCommand object.

适用于

另请参阅