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

定義

寫入一位元組序列至目前的 OracleLob,並依所寫入的位元組數目進階這個資料流裡的目前位置。

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

參數

buffer
Byte[]

位元組陣列。 這個方法會從 count 複製 buffer 中所指定的位元組數到目前資料流。

offset
Int32

buffer 中以零起始的位元組位移,即開始將位元組複製到目前資料流的位置。 對 CLOBNCLOB 資料型別而言,這必須是偶數。

count
Int32

寫入目前資料流的位元組數目。 對 CLOBNCLOB 資料型別而言,這必須是偶數。

例外狀況

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

offsetcount 參數中的值不是正數。

-或-

offsetcount 參數的總和大於 buffer 的長度。

-或-

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

-或-

您必須將 CLOBNCLOB 資料型別指定為偶數位元組數。

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

物件已關閉或處置。

發生 Oracle 錯誤。

備註

如果寫入作業成功,數據流內的位置會依寫入的位元元組數目前進。 如果發生例外狀況,數據流內的位置會保持不變。

允許超過 結尾 LOB 的寫入,並依寫入的位元組數目來放大 LOB

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

若要寫入 LOB,您必須已在 SQL SELECT 語句中使用 FOR UPDATE 子句擷取 LOB ,而且必須啟動本機交易。

下列範例示範如何寫入 OracleLob 物件:

public static void WriteLobExample(OracleCommand command)  
{  
    // Note: Updating LOB data requires a transaction.  
    command.Transaction = command.Connection.BeginTransaction();  
    // Select some data.  
    //    Table Schema:  
    //        "CREATE TABLE tablewithlobs (a int, b BLOB, c BLOB)";  
    //        "INSERT INTO tablewithlobs values (1, 'AA', 'AAA')";  
    command.CommandText = "SELECT * FROM TableWithLobs FOR UPDATE";  
    OracleDataReader reader = command.ExecuteReader();  
    using(reader)  
    {  
        // Obtain the first row of data.  
        reader.Read();  
        // Obtain both LOBs.  
        OracleLob BLOB1 = reader.GetOracleLob(1);  
        OracleLob BLOB2 = reader.GetOracleLob(2);  
        // Perform any desired operations on the LOB, (read, position, and so on).  
        // ...  
        // Example - Writing binary data (directly to the backend).  
        // To write, you can use any of the stream classes, or write raw binary data using   
        // the OracleLob write method. Writing character vs. binary is the same;  
        // however note that character is always in terms of Unicode byte counts  
        // (for example: even number of bytes - 2 bytes for every Unicode character).  
        var buffer = new byte[100];  
        buffer[0] = 0xCC;  
        buffer[1] = 0xDD;  
        BLOB1.Write(buffer, 0, 2);  
        BLOB1.Position = 0;  
        Console.WriteLine(BLOB1.LobType + ".Write(" + buffer + ", 0, 2) => " + BLOB1.Value);  

        // Example - Copying data into another LOB.  
        long actual = BLOB1.CopyTo(BLOB2);  
        Console.WriteLine(BLOB1.LobType + ".CopyTo(" + BLOB2.Value + ") => " + actual);  

        // Commit the transaction now that everything succeeded.  
        // Note: On error, Transaction.Dispose is called (from the using statement)  
        // and will automatically roll-back the pending transaction.  
        command.Transaction.Commit();  
    }  
}  

注意

唯讀 LOB 的寫入作業可能會成功,但不會更新 LOB 伺服器上的 。 不過,在此情況下,會更新的 LOB 本地副本。 因此,物件上稍後的 OracleLob 讀取作業可能會傳回寫入作業的結果。

適用於