OracleLob.Read(Byte[], Int32, Int32) 方法

定義

自目前 OracleLob 讀取一位元組序列,並依所讀取的位元組數目進階資料流裡的位置。

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

參數

buffer
Byte[]

位元組陣列。 當這個方法傳回時,緩衝區會包含指定的位元組陣列,這個陣列具有介於 offset 到 (offset + count) 之間的值,已由讀取自目前來源的位元組所取代。

offset
Int32

buffer 中以零起始的位元組位移,即開始儲存讀取自目前資料流之資料的位置。 對 CLOBNCLOB 資料型別而言,這必須是偶數。

count
Int32

自目前資料流讀取的最大位元組數。 對 CLOBNCLOB 資料型別而言,這必須是偶數。

傳回

緩衝區所讀取的總位元組數。 如果目前無法取得足夠的位元組,則這個數目可能小於所要求的位元組數;如果已經到達資料流末端,則為零 (0)。

例外狀況

buffer 為 null 參考 (在 Visual Basic 為 Nothing)。

offsetcount 參數中的值不是正數。

-或-

位移和計數參數的總和大於緩衝區的長度。

-或-

amountoffset 參數中所指定的值小於零,或是大於 4 GB。

作業不在交易中、OracleLob 物件為 null,或是連接已關閉。

物件已關閉或處置。

發生 Oracle 錯誤。

備註

方法Read會從目前的數據流讀取最多位元組,count並從 開始offset儲存它們buffer。 數據流中的目前位置會依讀取的位元元組數目進階;不過,如果發生例外狀況,數據流內的目前位置會保持不變。 Read 會傳回讀取的位元組數目。 只有在位置目前位於數據流結尾時,傳回值才會是零。 Read 將會封鎖,直到可以讀取至少一個字節的數據,否則不會有數據可供使用。Read 如果您嘗試從 LOB 讀取,則當目前位置位於 的 LOB結尾時,會傳回 0。 Read 即使尚未到達數據流結尾,也可以傳回比要求的位元組少。

.NET Framework Oracle 的數據提供者會將所有數據CLOBNCLOB處理為 Unicode。 因此,存取 CLOBNCLOB 數據類型時,您一律會處理位元組數目,其中每個字元都是 2 個字節。 例如,如果包含三個字元的文字字串儲存在 NCLOB Oracle 伺服器上,其中字元集是每一個字元 4 個字節,而您執行 Read 作業,則會將字元串的長度指定為 6 個字節,不過它會儲存為伺服器上的 12 個字節。

下列範例示範如何讀取 OracleLob 物件。

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);  
    }  
}  

您可以使用下列格式建構 OracleLob NULL 的 :

OracleLob myLob = OracleLob.Null;  

這項技術主要用於測試從伺服器傳回的 是否 LOB 為 NULL,如下列範例所示。

if (myLob == OracleLob.Null)  

NULL LOB 的行為類似於 中Read成功且一律傳回零位元組的零位元組LOB

適用於