ערוך

שתף באמצעות


OracleLob.Read(Byte[], Int32, Int32) Method

Definition

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

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

Parameters

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

Returns

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

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

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.

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

The object was closed or disposed.

An Oracle error has occurred.

Remarks

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 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).  
        var 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 manipulate streams can also be used. For example, the   
        // .NET StreamReader makes converting the raw bytes into actual characters easier.  
        var streamreader = new StreamReader(CLOB, Encoding.Unicode);  
        var 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.

Applies to