OracleCommand.ExecuteNonQuery 方法

定义

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

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

返回

对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于 CREATE TABLE 和 DROP TABLE 语句,返回值为 0。For CREATE TABLE and DROP TABLE statements, the return value is 0. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1.

异常

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

-or-

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

示例

下面的示例创建一个 OracleCommand,然后使用 ExecuteNonQuery执行它。The following example creates an OracleCommand and then executes it by 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 database.

public void CreateOracleCommand(string myExecuteQuery, string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        OracleCommand command = new OracleCommand(myExecuteQuery, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}
Public Sub CreateOracleCommand(ByVal myExecuteQuery As String, _
ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        Dim command As New OracleCommand(myExecuteQuery, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
End Sub

注解

您可以使用 ExecuteNonQuery 来执行目录操作(例如,查询数据库的结构或创建表等数据库对象);或者,通过执行 UPDATE、INSERT 或 DELETE 语句来更改数据库中的数据,而不使用 DataSetYou can use 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 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. 对于 CREATE TABLE 和 DROP TABLE 语句,返回值为 0。For CREATE TABLE and DROP TABLE statements, the return value is 0. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1. 如果发生回滚,则返回值也为-1。If a rollback occurs, the return value is also -1.

适用于

另请参阅