OleDbCommand.ExecuteNonQuery OleDbCommand.ExecuteNonQuery OleDbCommand.ExecuteNonQuery OleDbCommand.ExecuteNonQuery Method


针对 Connection 执行 SQL 语句并返回受影响的行数。Executes an SQL statement against the Connection and returns the number of rows affected.

 virtual int ExecuteNonQuery();
public int ExecuteNonQuery ();
override this.ExecuteNonQuery : unit -> int
Public Function ExecuteNonQuery () As Integer


受影响的行数。The number of rows affected.



连接不存在。The connection does not exist.


连接未打开。The connection is not open.


不能在连接最初在其中登记的上下文之外的事务上下文中执行命令。Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.


下面的示例创建一个OleDbCommand ,然后使用来ExecuteNonQuery执行它。The following example creates an OleDbCommand and then executes it using ExecuteNonQuery. 该示例向传递了一个字符串,该字符串是 SQL 语句,如 UPDATE、INSERT 或 DELETE 以及用于连接到数据源的字符串。The example is passed a string that is an SQL statement such as UPDATE, INSERT, or DELETE, and a string to use to connect to the data source.

static private void CreateOleDbCommand(
    string queryString, string connectionString)
    using (OleDbConnection connection = new 
        OleDbCommand command = new 
            OleDbCommand(queryString, connection);
Private Sub CreateOleDbCommand( _
    ByVal queryString As String, ByVal connectionString As String)
    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)
    End Using
End Sub


您可以使用ExecuteNonQuery执行目录操作,例如,查询数据库的结构或创建数据库对象(如表),或者DataSet通过执行 UPDATE、INSERT 或 DELETE 来更改数据库中的数据,而无需使用。前瞻性.You can use the ExecuteNonQuery to perform catalog operations, for example, to query the structure of a database or to create database objects such as tables, or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.

尽管未ExecuteNonQuery返回任何行,但映射到参数的任何输出参数或返回值都将用数据填充。Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data.

对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1. 如果发生回滚,则返回值也为-1。If a rollback occurs, the return value is also -1.