OracleLob.Read メソッド
メモ : この名前空間、クラス、およびメンバは、.NET Framework Version 1.1 だけでサポートされています。
現在の OracleLob ストリームからバイト シーケンスを読み取り、読み取ったバイト数の分だけストリーム内の位置を進めます。
Overrides Public Function Read( _
ByVal buffer() As Byte, _ ByVal offset As Integer, _ ByVal count As Integer _) As Integer
[C#]
public override int Read(byte[] buffer,intoffset,intcount);
[C++]
public: int Read(unsigned charbuffer __gc[],intoffset,intcount);
[JScript]
public override function Read(
buffer : Byte[],offset : int,count : int) : int;
パラメータ
- buffer
バイト配列。このメソッドが戻るとき、指定したバイト配列の offset から (offset + count) までの値が、現在のソースから読み取られたバイトに置き換えられて含まれます。 - offset
現在のストリームから読み取ったデータの格納を開始する位置を示す buffer 内のバイト オフセット。インデックス番号は 0 から始まります。 CLOB 型および NCLOB 型の場合、これは偶数である必要があります。 - count
現在のストリームから読み取る最大バイト数。 CLOB 型および NCLOB 型の場合、これは偶数である必要があります。
戻り値
バッファに読み取られた合計バイト数。要求しただけのバイト数を読み取ることができなかった場合、この値は要求したバイト数より小さくなります。ストリームの末尾に到達した場合は 0 になることがあります。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | buffer が null 参照 (Visual Basic の場合は Nothing) です。 |
ArgumentOutOfRangeException | offset パラメータまたは count パラメータの値が正の値ではありません。
または オフセット パラメータとカウント パラメータの合計値が、バッファ長を超えています。 または amount パラメータまたは offset パラメータに指定された値が 0 より小さいか、4 GB を超えています。 |
InvalidOperationException | LOB に書き込むには、トランザクション内でこの操作を実行する必要があります。
または OracleLob オブジェクトが null です。 または 接続が閉じています。 |
ObjectDisposedException | オブジェクトが閉じられているか、破棄されています。 |
OracleException | Oracle エラーが発生しました。 |
解説
Read メソッドは、現在のストリームから最大で count で指定したバイト数だけ読み込み、読み込んだバイトを buffer 内の offset で始まる位置に格納します。ストリームの現在位置が、読み込んだバイト数だけ進みます。ただし、例外が発生した場合は、ストリーム内の現在位置はそのまま変わりません。 Read は、読み込んだバイト数を返します。現在の位置がストリームの末尾である場合だけ、0 の値が返されます。読み取るデータがない場合、 Read は、最低 1 バイトのデータを読み取ることができるまでブロックされます。現在の位置が LOB の末尾の場合に LOB から読み取ろうとした場合、 Read は 0 を返します。ストリームの末尾に到達していない場合でも、 Read は、必要なバイト数未満のデータを返すことができます。
.NET Framework Data Provider for Oracle は、すべての CLOB データおよび NCLOB データを Unicode として処理します。したがって、 CLOB 型および NCLOB 型では 1 文字が 2 バイトとなるため、これらにアクセスするときは常に複数のバイトを扱うことになります。たとえば、3 文字から成る文字列が、1 文字が 4 バイトの文字セットを使用している Oracle サーバーに NCLOB として保存されていて、 Read 操作を実行する場合、文字列はサーバーに 12 バイトとして格納されていますが、文字列の長さは 6 バイトを指定します。
OracleLob オブジェクトを読み取る方法の C# の例を次に示します。
public static void ReadLobExample(OracleCommand cmd)
{
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')";
cmd.CommandText = "SELECT * FROM tablewithlobs";
OracleDataReader reader = cmd.ExecuteReader();
using(reader)
{
//Obtain the first row of data.
reader.Read();
//Obtain the LOBs (all 3 varieties).
OracleLob BLOB = reader.GetOracleLob(1/*0:based ordinal*/);
OracleLob CLOB = reader.GetOracleLob(2/*0:based ordinal*/);
OracleLob NCLOB = reader.GetOracleLob(3/*0:based ordinal*/);
//Example - Reading binary data (in chunks).
byte[] buffer = new byte[100];
while((actual = blob.Read(buffer, 0/*buffer offset*/, buffer.Length/*count*/)) >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 manipluate streams can also be used. For example, the
//.NET StreamReader makes converting the raw bytes into actual characters easier.
StreamReader streamreader = new StreamReader(clob, Encoding.Unicode);
char[] cbuffer = new char[100];
while((actual = streamreader.Read(cbuffer, 0/*buffer offset*/, cbuffer.Length/*count*/)) >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);
}
}
この形式を使用して、NULL の OracleLob を構築できます。
OracleLob myLob = OracleLob.Null;
この手法は主に、この例が示すように、サーバーから返された LOB が NULL かどうかをテストするために使用します。
if( myLob == OracleLob.Null)
NULL の LOB は、0 バイトの LOB と同じように動作します。つまり、 Read は成功し、常に 0 バイトを返します。
必要条件
プラットフォーム: 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 名前空間