oracle 尝试读取 blob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4921423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:39:56  来源:igfitidea点击:

Trying to read a blob

c#oracleblob

提问by Kasper Hansen

I am trying to read a BLOB from an Oracle database. The function GetFileContent take p_file_id as a paramater and return a BLOB. The BLOB is a DOCX-file that needs to be written in a folder somewhere. But I can't quite figure out how to read the BLOB. There is definitely something stored in the return_value-paramater after

我正在尝试从 Oracle 数据库中读取 BLOB。函数 GetFileContent 以 p_file_id 作为参数并返回一个 BLOB。BLOB 是一个 DOCX 文件,需要写入某个文件夹中。但我不太清楚如何读取 BLOB。肯定有东西存储在 return_value-paramater 之后

OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

The value is {byte[9946]}. But I get an error when executing

值为 {byte[9946]}。但是我在执行时出错

long retrievedBytes = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize);

It says InvalidOperationException was caught: "No data exists for the row or column."

它说 InvalidOperationException 被捕获:“行或列不存在数据。”

Here is the code:

这是代码:

cmd = new OracleCommand("GetFileContent", oraCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_file_id", OracleType.Number).Direction = ParameterDirection.Input;
cmd.Parameters[0].Value = fileID;
cmd.Parameters.Add("return_value", OracleType.Blob).Direction = ParameterDirection.ReturnValue;
cmd.Connection.Open();

OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
reader.Read();

MemoryStream memory = new MemoryStream();
long startIndex = 0;
const int ChunkSize = 256;
while (true)
{
   byte[] buffer = new byte[ChunkSize];
   long retrievedBytes = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize); //FAILS
   memory.Write(buffer, 0, (int)retrievedBytes);
   startIndex += retrievedBytes;
   if (retrievedBytes != ChunkSize)
      break;
}
cmd.Connection.Close();
byte[] data = memory.ToArray();
memory.Dispose();

How can I read the BLOB from the function?

如何从函数中读取 BLOB?

采纳答案by Mike Ohlsen

Looks like you are using the Microsoft Oracle Client. You probably want to use the LOB objects rather than using GetBytes(...).

看起来您正在使用 Microsoft Oracle Client。您可能想要使用 LOB 对象而不是使用 GetBytes(...)。

I think the first link below would be the easiest for you. Here is an excerpt:

我认为下面的第一个链接对您来说是最简单的。这是摘录:

using(reader)
{
      //Obtain the first row of data.
      reader.Read();
      //Obtain the LOBs (all 3 varieties).
      OracleLob BLOB = reader.GetOracleLob(1);
      ...

      //Example - Reading binary data (in chunks).
      byte[] buffer = new byte[100];
      while((actual = BLOB.Read(buffer, 0, buffer.Length)) >0)
         Console.WriteLine(BLOB.LobType + ".Read(" + buffer + ", " + buffer.Length + ") => " + actual);

      ...
}

OracleLob::Read Method

OracleLob::Read 方法

OracleLob Class

OracleLob 类

OracleDataReader::GetOracleLob Method

OracleDataReader::GetOracleLob 方法

On a side note, the Microsoft Oracle client is being depreciated. You may want look into switching to Oracle's ODP.net, as that will be the only "Officially Supported" client moving forward.

附带说明一下,Microsoft Oracle 客户端正在贬值。您可能需要考虑切换到 Oracle 的 ODP.net,因为这将是唯一向前发展的“官方支持”客户端。