OdbcDataAdapter.InsertCommand 属性

获取或设置 SQL 语句或存储过程,用于将新记录插入到数据源中。

**命名空间:**System.Data.Odbc
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Property InsertCommand As OdbcCommand
用法
Dim instance As OdbcDataAdapter
Dim value As OdbcCommand

value = instance.InsertCommand

instance.InsertCommand = value
public OdbcCommand InsertCommand { get; set; }
public:
property OdbcCommand^ InsertCommand {
    OdbcCommand^ get ();
    void set (OdbcCommand^ value);
}
/** @property */
public OdbcCommand get_InsertCommand ()

/** @property */
public void set_InsertCommand (OdbcCommand value)
public function get InsertCommand () : OdbcCommand

public function set InsertCommand (value : OdbcCommand)

属性值

在更新操作期间使用的 OdbcCommand,它用于将与 DataSet 中的新行相对应的记录插入到数据源中。

备注

向以前创建的 OdbcCommand 对象分配 InsertCommand 属性时,不会克隆 OdbcCommand。相反,InsertCommand 维护对以前创建的 OdbcCommand 的引用。

更新操作期间,如果没有设置 InsertCommand 并且在 DataSet 中存在主键信息,可以使用 OdbcCommandBuilder 类来自动生成 InsertCommand 和协调 DataSet 与数据源所需的附加命令。要实现这一点,请设置 OdbcDataAdapterSelectCommand 属性。此生成逻辑还要求 DataSet 中存在键列信息。有关更多信息,请参见 自动生成命令

提示

如果执行此命令后返回行,则这些行可能会添加到 DataSet 中,具体取决于如何设置 OdbcCommand 对象的 .Data.Odbc.OdbcCommand.UpdatedRowSource 属性。

示例

下面的示例创建一个 OdbcDataAdapter 并设置 SelectCommandInsertCommand 属性。假定已经创建了一个 OdbcConnection 对象。

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
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;
}

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1

请参见

参考

OdbcDataAdapter 类
OdbcDataAdapter 成员
System.Data.Odbc 命名空间

其他资源

使用 DataAdapter