ExecuteNonQueryAsync(CancellationToken cancellationToken)

This method returns an asynchronous version of ExecuteNonQuery(), which executes the command against its connection object and returns the number of rows affected with a CancellationToken.

Declaration

// C#
public override Task<int> int ExecuteNonQueryAsync(CancellationToken cancellationToken);

Return Value

A Task object representing the asynchronous operation immediately without blocking the calling thread for the whole duration of the query execution.

Implements

DbCommand

Exceptions

InvalidOperationException - The command cannot be executed.

Example

using Oracle.ManagedDataAccess.Client;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncApp
{
  class AsyncDemo
  {
    static async Task Main()
    {
      string connection = "User Id=HR; Password=<PASSWORD>; Data Source=oracle;";
      OracleConnection oc = new OracleConnection(connection);
      await oc.OpenAsync(CancellationToken.None);
      Console.WriteLine("Connection opened successfully");

      OracleCommand cmd = oc.CreateCommand();
      cmd.CommandText = "insert into tab1 values(1)";
      Task task = cmd.ExecuteNonQueryAsync(CancellationToken.None);
      await task;
      Console.WriteLine("Query executed");
      oc.Close();
    }
  }
}