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[]

字节数组。 此方法将指定字节数 countbuffer 当前流复制到当前流。

offset
Int32

从零开始的字节偏移量 buffer ,开始将字节复制到当前流。 对于 CLOB 数据类型和 NCLOB 数据类型,这必须是偶数。

count
Int32

要写入当前流的字节数。 对于 CLOB 数据类型和 NCLOB 数据类型,这必须是偶数。

例外

buffer 参数是空引用(Visual Basic 中的 Nothing)。

offset参数中的count值不是正值。

-或-

和参数的总 offsetcount 大于 buffer 长度。

-或-

countoffset 参数中指定的值小于零或大于 4 千兆字节。

-或-

必须将数据类型指定 CLOBNCLOB 偶数字节。

该操作不在事务中,对象 OracleLob 为 null,或连接已关闭。

对象已关闭或释放。

发生 Oracle 错误。

注解

如果写入操作成功,流中的位置会按写入的字节数向前推进。 如果发生异常,则流中的位置保持不变。

允许超出末尾 LOB 的写入,并放大 LOB 写入的字节数。

Oracle 的 .NET Framework Data Provider 将所有 CLOBNCLOB 数据作为 Unicode 进行处理。 因此,访问 CLOBNCLOB 数据类型时,始终处理字节数,其中每个字符为 2 个字节。 例如,如果包含三个字符的文本字符串保存在 NCLOB 每个字符为 4 个字节的 Oracle 服务器上,并且执行 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 对象的读取操作可能会返回写入操作的结果。

适用于