IDbCommand.ExecuteNonQuery IDbCommand.ExecuteNonQuery IDbCommand.ExecuteNonQuery IDbCommand.ExecuteNonQuery Method


对 .NET Framework 数据提供程序的 Connection 对象执行 SQL 语句,并返回受影响的行数。Executes an SQL statement against the Connection object of a .NET Framework data provider, and returns the number of rows affected.

 int ExecuteNonQuery();
public int ExecuteNonQuery ();
abstract member ExecuteNonQuery : unit -> int
Public Function ExecuteNonQuery () As Integer


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


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


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


下面的示例创建派生类OleDbCommand的实例, 然后执行它。The following example creates an instance of the derived class, OleDbCommand, and then executes it. 为实现此目的, 向方法传递了一个字符串, 该字符串是一个 SQL SELECT 语句和一个用于连接到数据源的字符串。To accomplish this, the method is passed a string that is a SQL SELECT statement 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, querying the structure of a database or creating 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 does not return any 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.