ReadAsync(byte[], int, int, CancellationToken)

The ReadAsync method returns a Task-based asynchronous version of OracleBlob.Read(), which reads a specified number of bytes from the ODP.NET LOB instance and populates the buffer.

Declaration

// C#
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken);

Parameters

  • buffer

    The byte array buffer to be populated.

  • offset

    The starting offset (in bytes) at which the buffer is populated.

  • count

    The number of bytes to read.

  • cancellationToken

    The input cancellation token which can be used by the application to cancel the task.

Return Value

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

Exceptions

  • ObjectDisposedException - The object is already disposed.

  • InvalidOperationException - The OracleConnection is not open or has been closed during the lifetime of the object
  • ArgumentOutOfRangeException - This exception is thrown if any of the following conditions exist:

    • The offset or the count parameter is less than 0.

    • The offset is greater than or equal to the buffer.Length.

    • The offset and the count together are greater than the buffer.Length.

Example

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

namespace AsyncApp
{
  class AsyncDemo
  {
    static async Task Main(string[] args)
    {
      string connectionString = "User Id=HR; Password=<PASSWORD>; Data Source=oracle;";

      OracleConnection oc = new OracleConnection(connectionString);
      await oc.OpenAsync(CancellationToken.None);

      OracleCommand cmd = oc.CreateCommand();
      cmd.CommandText = " select blob_column from tab1";


      OracleDataReader reader = await cmd.ExecuteReaderAsync();

      while (await reader.ReadAsync(CancellationToken.None))
      {
        using (OracleBlob blob = reader.GetOracleBlob(0))
        {
          byte[] buffer = new byte[100];
          //asynchronously read blob data
          Task<int> task = blob.ReadAsync(buffer, 0, 64, CancellationToken.None);

          //other operations
          Console.WriteLine("Hello World");

          //await for asynchronous ReadAsync 
          int bytesRead = await task;
          Console.WriteLine("Bytes Read: " + bytesRead);
        }
      }
    }
  }
}