次の方法で共有


OracleLob.Write メソッド

メモ : この名前空間、クラス、およびメンバは、.NET Framework Version 1.1 だけでサポートされています。

バイト シーケンスを現在の OracleLob ストリームに書き込み、書き込んだバイト数の分だけストリーム内の現在位置を進めます。

Overrides Public Sub Write( _
   ByVal buffer() As Byte, _   ByVal offset As Integer, _   ByVal count As Integer _)
[C#]
public override void Write(byte[] buffer,intoffset,intcount);
[C++]
public: void Write(unsigned charbuffer __gc[],intoffset,intcount);
[JScript]
public override function Write(
   buffer : Byte[],offset : int,count : int);

パラメータ

  • buffer
    バイト配列。このメソッドは、 buffer から現在のストリームに、 count で指定されたバイト数だけコピーします。
  • offset
    現在のストリームへのバイトのコピーを開始する位置を示す buffer 内のバイト オフセット。インデックス番号は 0 から始まります。 CLOB 型および NCLOB 型の場合、これは偶数である必要があります。
  • count
    現在のストリームに書き込むバイト数。 CLOB 型および NCLOB 型の場合、これは偶数である必要があります。

例外

例外の種類 条件
ArgumentNullException buffer パラメータが null 参照 (Visual Basic の場合は Nothing) です。
ArgumentOutOfRangeException offset パラメータまたは count パラメータの値が正の値ではありません。

または

offset パラメータと count パラメータの合計値が、 buffer の長さを超えています。

または

count パラメータまたは offset パラメータに指定された値が 0 より小さいか、4 GB を超えています。

または

CLOB および NCLOB データ型には、偶数のバイト数を指定する必要があります。

InvalidOperationException LOB に書き込むには、トランザクション内でこの操作を実行する必要があります。

または

OracleLob オブジェクトが null です。

または

接続が閉じています。

ObjectDisposedException オブジェクトが閉じられているか、破棄されています。
OracleException Oracle エラーが発生しました。

解説

書き込み操作が成功した場合は、書き込まれたバイト数だけストリーム内の位置が進みます。例外が発生した場合は、ストリーム内の位置はそのまま変わりません。

LOB の末尾を超えて書き込むことができます。その場合、 LOB は、書き込んだバイト数だけ拡大されます。

.NET Framework Data Provider for Oracle は、すべての CLOB データおよび NCLOB データを Unicode として処理します。したがって、 CLOB 型および NCLOB 型では 1 文字が 2 バイトとなるため、これらにアクセスするときは常に複数のバイトを扱うことになります。たとえば、3 文字から成る文字列が、1 文字が 4 バイトの文字セットを使用している Oracle サーバーに NCLOB として保存されていて、 Write 操作を実行する場合、文字列はサーバーに 12 バイトとして格納されていますが、文字列の長さは 6 バイトを指定します。

LOB に書き込むには、SQL SELECT ステートメントで FOR UPDATE 句を使用して LOB を取得しておく必要があります。

OracleLob オブジェクトに書き込む方法の C# の例を次に示します。

public static void WriteLobExample(OracleCommand cmd)
{
    //Note: Updating LOB data requires a transaction.
    cmd.Transaction = cmd.Connection.BeginTransaction();
        
    //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')";
    cmd.CommandText = "SELECT * FROM tablewithlobs FOR UPDATE";
    OracleDataReader reader = cmd.ExecuteReader();
    using(reader)
    {
        //Obtain the first row of data.
        reader.Read();
        //Obtain a LOB.
        OracleLob BLOB    = reader.GetOracleLob(1/*0:based ordinal*/);
        //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).
        byte[] buffer = new byte[100];
        buffer[0] = 0xCC;
        buffer[1] = 0xDD;
        BLOB.Write(buffer, 0/*buffer offset*/, 2/*count*/);
        BLOB.Position = 0;
        Console.WriteLine(BLOB.LobType + ".Write(" + buffer + ", 0, 2) => " + BLOB.Value);
            
        //Example - Obtaining a temp LOB and copying data into it from another LOB.
        OracleLob templob = CreateTempLob(cmd, BLOB.LobType);
        long actual = BLOB.CopyTo(templob);
        Console.WriteLine(BLOB.LobType + ".CopyTo(" + templob.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.
        cmd.Transaction.Commit();
    }
}

メモ   このリリースでは、読み取り専用の LOB に対する書き込み操作が成功する可能性がありますが、サーバー上の LOB は更新されません。ただし、この場合、 LOB のローカル コピーは更新されます。したがって、 OracleLob オブジェクトに対するその後の読み取り操作では、書き込み操作の結果が返される可能性があります。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

OracleLob クラス | OracleLob メンバ | System.Data.OracleClient 名前空間