Export (0) Print
Expand All

OracleLob.Read Method

Reads a sequence of bytes from the current OracleLob stream and advances the position within the stream by the number of bytes read.

Namespace:  System.Data.OracleClient
Assembly:  System.Data.OracleClient (in System.Data.OracleClient.dll)

public override int Read(
	byte[] buffer,
	int offset,
	int count
)

Parameters

buffer
Type: System.Byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count) replaced by the bytes read from the current source.

offset
Type: System.Int32

The zero-based byte offset in buffer at which to begin storing the data read from the current stream. For CLOB and NCLOB data types, this must be an even number.

count
Type: System.Int32

The maximum number of bytes to be read from the current stream. For CLOB and NCLOB data types, this must be an even number.

Return Value

Type: System.Int32
The total number of bytes read into the buffer. This may be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

ExceptionCondition
ArgumentNullException

The buffer is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

A value in the offset or count parameter is not positive.

-or-

The sum of the offset and count parameters is larger than the buffer length.

-or-

A value specified in the amount or offset parameter is less than zero or greater than 4 gigabytes.

InvalidOperationException

The operation is not within a transaction, the OracleLob object is null, or the connection is closed.

ObjectDisposedException

The object was closed or disposed.

OracleException

An Oracle error has occurred.

The Read method reads a maximum of count bytes from the current stream and stores them in buffer beginning at offset. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. Read returns the number of bytes read. The return value is zero only if the position is currently at the end of the stream. Read will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 if you attempt to read from a LOB when the current position is at the end of LOB. Read can return fewer bytes than requested even if the end of the stream has not been reached.

The .NET Framework Data Provider for Oracle handles all CLOB and NCLOB data as Unicode. Therefore, when accessing CLOB and NCLOB data types, you are always dealing with the number of bytes, where each character is 2 bytes. For example, if a string of text containing three characters is saved as an NCLOB on an Oracle server where the character set is 4 bytes per character, and you perform a Read operation, you specify the length of the string as 6 bytes, although it is stored as 12 bytes on the server.

The following C# example demonstrates how to read OracleLob objects.

public static void ReadLobExample(OracleCommand command)
{
   int actual = 0;

   //Select some data.
   // Table Schema:
   //  "CREATE TABLE TableWithLobs (a int, b BLOB, c CLOB, d NCLOB)";
   //  "INSERT INTO TableWithLobs values (1, 'AA', 'AAA', N'AAAA')";
   command.CommandText = "SELECT * FROM TableWithLobs";
   OracleDataReader reader = command.ExecuteReader();
   using(reader)
   {
      //Obtain the first row of data.
      reader.Read();
      //Obtain the LOBs (all 3 varieties).
      OracleLob BLOB = reader.GetOracleLob(1);
      OracleLob CLOB  = reader.GetOracleLob(2);
      OracleLob NCLOB = reader.GetOracleLob(3);

      //Example - Reading binary data (in chunks).
      byte[] buffer = new byte[100];
      while((actual = BLOB.Read(buffer, 0, buffer.Length)) >0)
         Console.WriteLine(BLOB.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);

      //Example - Reading CLOB/NCLOB data (in chunks).
      //Note: You can read character data as raw Unicode bytes (using OracleLob.Read as in the above example).
      //However, because the OracleLob object inherits directly from the.NET stream object, 
      //all the existing classes that manipluate streams can also be used. For example, the 
      //.NET StreamReader makes converting the raw bytes into actual characters easier.
      StreamReader streamreader = new StreamReader(CLOB, Encoding.Unicode);
      char[] cbuffer = new char[100];
      while((actual = streamreader.Read(cbuffer, 0, cbuffer.Length)) >0)
         Console.WriteLine(CLOB.LobType + ".Read(" + new string(cbuffer, 0, actual) + ", " + cbuffer.Length + ") => " + actual);

      //Example - Reading data (all at once).
      //You could use StreamReader.ReadToEnd to obtain all the string data,or simply
      //call OracleLob.Value to obtain a contiguous allocation of all the data.
      Console.WriteLine(NCLOB.LobType + ".Value => " + NCLOB.Value);
   }
}

You can construct an OracleLob that is NULL using this format:

OracleLob myLob = OracleLob.Null;

This technique is used primarily to test whether a LOB returned from the server is NULL, as the following example illustrates.

If(myLob == OracleLob.Null)

A NULL LOB behaves similarly to a zero byte LOB in that Read succeeds and always returns zero bytes.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft