This documentation is archived and is not being maintained.

OracleLob.Read Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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

[Visual Basic]
Overrides Public Function Read( _
   ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
) As Integer
[C#]
public override int Read(
 byte[] buffer,
 int offset,
 int count
);
[C++]
public: int Read(
 unsigned char buffer __gc[],
 int offset,
 int count
);
[JScript]
public override function Read(
   buffer : Byte[],
 offset : int,
 count : int
) : int;

Parameters

buffer
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
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
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

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.

Exceptions

Exception Type Condition
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 Writing to a LOB requires that the operation be within a transaction.

-or-

The OracleLob object is null.

-or-

The connection is closed.

ObjectDisposedException The object was closed or disposed.
OracleException An Oracle error has occurred.

Remarks

The Read method read a maximum of count bytes from the current stream and store 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 is free to 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 cmd)
{
 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')";
 cmd.CommandText   = "SELECT * FROM tablewithlobs";
 OracleDataReader reader = cmd.ExecuteReader();
 using(reader)
 {
  //Obtain the first row of data.
  reader.Read();
  
  //Obtain the LOBs (all 3 varieties).
  OracleLob BLOB = reader.GetOracleLob(1/*0:based ordinal*/);
  OracleLob CLOB  = reader.GetOracleLob(2/*0:based ordinal*/);
  OracleLob NCLOB = reader.GetOracleLob(3/*0:based ordinal*/);
  //Example - Reading binary data (in chunks).
  byte[] buffer = new byte[100];
  while((actual = blob.Read(buffer, 0/*buffer offset*/, buffer.Length/*count*/)) >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/*buffer offset*/, cbuffer.Length/*count*/)) >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 this 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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

OracleLob Class | OracleLob Members | System.Data.OracleClient Namespace

Show: