OracleCommand.ExecuteNonQuery Method

Definition

Executes an SQL statement against the Connection and returns the number of rows affected.

C#
public int ExecuteNonQuery();
C#
public override int ExecuteNonQuery();

Returns

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For CREATE TABLE and DROP TABLE statements, the return value is 0. For all other types of statements, the return value is -1.

Implements

Exceptions

The connection does not exist.

-or-

The connection is not open.

Examples

The following example creates an OracleCommand and then executes it by using ExecuteNonQuery. 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.

C#
public void CreateOracleCommand(string myExecuteQuery, string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        OracleCommand command = new OracleCommand(myExecuteQuery, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

Remarks

You 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.

Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For CREATE TABLE and DROP TABLE statements, the return value is 0. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also