Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索BLOB数据时出错。由于对象的当前状态,操作无效_C#_Oracle_Bytearray_Blob_Memorystream - Fatal编程技术网

C# 检索BLOB数据时出错。由于对象的当前状态,操作无效

C# 检索BLOB数据时出错。由于对象的当前状态,操作无效,c#,oracle,bytearray,blob,memorystream,C#,Oracle,Bytearray,Blob,Memorystream,我有以下C#代码,用于将BLOB类型的数据从ORACLE检索到字节[] 代码: private MemoryStream GetStatement(int loginId, OracleConnection con) { var memoryStream = new MemoryStream(); using (var oraQuery = new OracleCommand(@"SELECT statement_file from user_accoun

我有以下C#代码,用于将BLOB类型的数据从ORACLE检索到
字节[]

代码:

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;
}