OleDbDataAdapter.InsertCommand 属性

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

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

语法

声明
Public Property InsertCommand As OleDbCommand
用法
Dim instance As OleDbDataAdapter
Dim value As OleDbCommand

value = instance.InsertCommand

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

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

public function set InsertCommand (value : OleDbCommand)

属性值

Update 过程中使用的 OleDbCommand,用于将与 DataSet 中的新行相对应的记录插入到数据源中。

备注

Update 过程中,如果未设置此属性而且 DataSet 中存在主键信息,那么在设置 SelectCommand 属性并使用 OleDbCommandBuilder 的情况下,可以自动生成 InsertCommand。然后,OleDbCommandBuilder 将生成其他任何未设置的命令。此生成逻辑要求 DataSet 中存在键列信息。有关更多信息,请参见 自动生成命令

在将 InsertCommand 分配给以前创建的 OleDbCommand 时,不克隆 OleDbCommandInsertCommand 维护对以前创建的 OleDbCommand 对象的引用。

提示

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

示例

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

Public Shared Function CreateCustomerAdapter( _
    connection As OleDbConnection) As OleDbDataAdapter 
  
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
    Dim command As OleDbCommand

    ' Create the SelectCommand.
    command = New OleDbCommand("SELECT CustomerID FROM Customers " & _
        "WHERE Country = ? AND City = ?", connection)

    command.Parameters.Add("Country", OleDbType.VarChar, 15)
    command.Parameters.Add("City", OleDbType.VarChar, 15)

    adapter.SelectCommand = command

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

    command.Parameters.Add( _
        "CustomerID", OleDbType.Char, 5, "CustomerID")
    command.Parameters.Add( _
        "CompanyName", OleDbType.VarChar, 40, "CompanyName")

    adapter.InsertCommand = command
    Return adapter
End Function
public static OleDbDataAdapter CreateCustomerAdapter(
    OleDbConnection connection)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command;

    // Create the SelectCommand.
    command = new OleDbCommand("SELECT CustomerID FROM Customers " +
        "WHERE Country = ? AND City = ?", connection);

    command.Parameters.Add("Country", OleDbType.VarChar, 15);
    command.Parameters.Add("City", OleDbType.VarChar, 15);

    adapter.SelectCommand = command;

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

    command.Parameters.Add(
        "CustomerID", OleDbType.Char, 5, "CustomerID");
    command.Parameters.Add(
        "CompanyName", OleDbType.VarChar, 40, "CompanyName");

    adapter.InsertCommand = command;
    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、1.0

请参见

参考

OleDbDataAdapter 类
OleDbDataAdapter 成员
System.Data.OleDb 命名空间

其他资源

使用 DataAdapter