使用Oracle.ManagedDataAccess读取全局临时表

本文关键字:全局 临时表 读取 ManagedDataAccess Oracle 使用 | 更新日期: 2023-09-27 18:20:26

我们使用的存储过程涉及多个全局临时表和PL SQL数组,以避免使用动态SQL查询,并且由于我们可能返回大量记录。然而,当我们试图从结果中读取时,我们遇到了一个小问题。

我们过去的模式是使用如下代码从结果中读取:

DataTable result = new DataTable();
const string procName = "some proc";
using (var connection = new OracleConnection("some connection string"))
using (var command = new OracleCommand(procName, connection))
{
   command.CommandType = CommandType.StoredProcedure;
   command.BindByName = true;
   command.Parameters.Add(new OracleParameter("my_ref_cursor", OracleDbType.RefCursor)
   {
      Direction = ParameterDirection.Output
   });
   connection.Open();
   using (var reader = command.ExecuteReader())
   {
     while (reader.Read())
     {
         // some logic here
     }
   }
   connection.Close();
}

然而,当我们试图从读取器读回结果时,它会跳过解析循环,就好像没有数据一样。当我们在TOAD中复制调用时,我们可以毫无问题地返回结果。

我们已经猜测,当我们试图读回结果时,驱动程序正在执行提交,但我们不确定如何绕过这一点。我们已经尝试使用以下内容:

connection.BeginTransaction(IsolationLevel.ReadUncommitted);

但这带来了一个可爱的例外,虽然很明确,但并不能帮助我真正解决问题。

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=IsolationLevel must be ReadCommitted or Serializable
Parameter name: isolationLevel
  Source=Oracle.ManagedDataAccess
  ParamName=isolationLevel
  StackTrace:
       at Oracle.ManagedDataAccess.Client.OracleConnection.BeginTransaction(IsolationLevel isolationLevel)

有没有想过我们是否能做我们想做的事?

使用Oracle.ManagedDataAccess读取全局临时表

如错误消息中所述,不允许使用隔离级别IsolationLevel.ReadUncommitted。必须使用ReadCommitted(默认值)或Serializable

您是否使用ON COMMIT DELETE ROWS(默认值)创建了全局临时表?

尝试使用创建表格

CREATE GLOBAL TEMPORARY TABLE ....
(
...
)
ON COMMIT PRESERVE ROWS;