检索BLOB数据时出错.由于对象的当前状态,操作无效
本文关键字:状态 无效 操作 对象 数据 BLOB 出错 于对象 检索 | 更新日期: 2023-09-27 18:21:41
我在C#中有以下代码,用于将BLOB类型的数据从ORACLE检索到byte[]
中。
代码:
private MemoryStream GetStatement(int loginId, OracleConnection con)
{
var memoryStream = new MemoryStream();
using (var oraQuery = new OracleCommand(@"SELECT statement_file from user_account_statement where login_id=" + loginId + "", con))
{
using (var oraQueryResult = oraQuery.ExecuteReader())
if (oraQueryResult != null)
{
oraQueryResult.Read();
var blob = new Byte[(oraQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
oraQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
//updated.
memoryStream.Write(blob, 0, blob.Length);
}
}
return memoryStream;
}
错误
由于对象的当前状态,操作无效。
在代码行:
var blob = new Byte[(oraQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
通过参数化查询解决的问题。以下工作代码:
private static MemoryStream GetStatement(OracleConnection con, int loginId)
{
var memoryStream = new MemoryStream();
using (
var oraQuery = new OracleCommand(@"SELECT statement_file from user_account_statement where login_id=:1, con))
{
oraQuery.BindByName = true;
oraQuery.Parameters.Add(":1", OracleDbType.Int32).Value = loginId;
using (var oraQueryResult = oraQuery.ExecuteReader())
if (oraQueryResult != null)
{
while (oraQueryResult.Read())
{
var blob = new Byte[(oraQueryResult.GetBytes(0, 0, null, 0, int.MaxValue))];
oraQueryResult.GetBytes(0, 0, blob, 0, blob.Length);
memoryStream.Write(blob, 0, blob.Length);
}
}
}
return memoryStream;
}