OracleLob.Read(Byte[], Int32, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从当前 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[]
字节数组。 此方法返回时,缓冲区包含指定的字节数组,这些offsetoffset + count字节数组的值由从当前源中读取的字节替换。
- offset
- Int32
从零开始的字节偏移量 buffer ,从中开始存储从当前流中读取的数据。 对于 CLOB 数据类型和 NCLOB 数据类型,这必须是偶数。
- count
- Int32
要从当前流中读取的最大字节数。 对于 CLOB 数据类型和 NCLOB 数据类型,这必须是偶数。
返回
读取到缓冲区中的字节总数。 如果当前没有多少个字节可用,则这可能小于请求的字节数;如果已达到流的末尾,则为零(0)。
例外
buffer 是空引用(Visual Basic 中的 Nothing)。
或offset参数中的count值不是正值。
-或-
偏移量和计数参数的总和大于缓冲区长度。
-或-
参数 offset 中指定的值小于零或大于 4 GB。
该操作不在事务中,对象 OracleLob 为 null,或连接已关闭。
对象已关闭或释放。
发生 Oracle 错误。
注解
该方法 Read 从当前流中读取最大字节数 count ,并将其存储在 buffer 开头 offset。 流中的当前位置按读取的字节数进行高级;但是,如果发生异常,流中的当前位置保持不变。
Read 返回读取的字节数。 仅当位置当前位于流末尾时,返回值为零。
Read 在没有任何数据可用的情况下,将阻止数据,直到可以读取至少一个字节的数据。Read 如果尝试从 LOB 当前位置位于末尾 LOB时读取,则返回 0。
Read 即使尚未到达流的末尾,也可以返回比请求的字节少。
Oracle 的 .NET Framework Data Provider 将所有 CLOB 和 NCLOB 数据作为 Unicode 进行处理。 因此,访问 CLOB 和 NCLOB 数据类型时,始终处理字节数,其中每个字符为 2 个字节。 例如,如果包含三个字符的文本字符串保存在 NCLOB 每个字符为 4 个字节的 Oracle 服务器上,并且执行 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;
此方法主要用于测试从服务器返回的值为 LOB NULL,如以下示例所示。
if (myLob == OracleLob.Null)
NULL LOB 的行为类似于成功LOB且始终返回零字节的零字节Read。