OleDbCommand.ExecuteNonQuery Method

Definition

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

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

Returns

The number of rows affected.

Implements

Exceptions

The connection does not exist.

-or-

The connection is not open.

-or-

Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

Examples

The following example creates an OleDbCommand and then executes it 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 data source.

C#
static private void CreateOleDbCommand(
    string queryString, string connectionString)
{
    using (OleDbConnection connection = new
               OleDbConnection(connectionString))
    {
        connection.Open();
        OleDbCommand command = new
            OleDbCommand(queryString, connection);
        command.ExecuteNonQuery();
    }
}

Remarks

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.

Although the ExecuteNonQuery returns no 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 all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Applies to

Toode Versioonid
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.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
.NET Standard 2.0 (package-provided)

See also