語言

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 ,從該位元組開始儲存從當前串流讀取的資料。 對於 CLOB 和 NCLOB 資料型態,這個數字必須是偶數。

count
Int32

目前串流中最大可讀取的位元組數。 對於 CLOB 和 NCLOB 資料型態,這個數字必須是偶數。

傳回

讀取到緩衝區的總位元組數。 若目前沒有該數量的位元組可用,這可能少於請求的位元組數;若已到達串流末端,則為零(0)。

例外狀況

是空參考()。

或offset參數中的count值並非正值。

-或-

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

-或-

參數中指定的 offset 值小於零或大於4 GB。

操作不在交易中、 OracleLob 物件為空,或連線已關閉。

該物件被封閉或棄置。

發生了神諭錯誤。

備註

此Read方法從目前串流讀取最多count位元組,並從 開始buffer儲存在 offset 。 串流中的當前位置會依讀取的位元組數前進;但若發生例外,串流中當前位置保持不變。 Read 回傳讀取的位元組數。 只有當該位置目前位於串流的末端時,回傳值才為零。 Read 若無法讀取資料,將阻塞直到至少讀取一個位元組的資料。Read 若你嘗試從 A LOB 讀取,而當前位置位於 的 LOB末端,則回傳 0。 Read 即使尚未到達串流的終點,也可能回傳比請求的位元組少。

Oracle 的 .NET Framework Data Provider 以 Unicode 形式處理所有 CLOB 和 NCLOB 資料。 因此,存取 CLOB 資料 NCLOB 型別時,你總是處理位元組數,每個字元為 2 位元組。 例如,若一串包含三個字元的文字串被儲存為 , NCLOB 且該伺服器的字元集為每個字元為 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;

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

if (myLob == OracleLob.Null)

NULL LOB 的行為類似於 中一個零位元組 LOB ,成功 Read 且總是回傳零位元組。

適用於