【问题标题】:Get 'PL\SQL Table' returned from a function with OracleCommand in C#使用 C# 中的 OracleCommand 获取从函数返回的“PL\SQL 表”
【发布时间】:2015-06-14 13:07:59
【问题描述】:

我不熟悉在 C# 中使用 OracleCommand 从 Oracle 过程\函数返回结果,我已经能够让我的大部分存储过程执行工作,但我需要以下方面的建议。

下面是一个函数,它返回我根据记录类型创建的表

create or replace
function return_table return t_nested_table AS

v_ret   t_nested_table;

begin
  v_ret  := t_nested_table();

  v_ret.extend;
  v_ret(v_ret.count) := t_col(1, 'one');

  v_ret.extend;
  v_ret(v_ret.count) := t_col(2, 'two');

  v_ret.extend;
  v_ret(v_ret.count) := t_col(3, 'three');

  return v_ret;
end return_table;

类型创建如下

create or replace
type t_col as object (
  i number,
  n varchar2(30)
);

来自 t_col 记录的表

create or replace
type t_nested_table as table of t_col;

现在,当我想在 C# 中执行该函数时,我尝试了以下方法,但发现 OracleDbType 没有 PL\SQL 表的枚举。

using (OracleConnection conn = new OracleConnection(connection))
            using (OracleCommand cmd = new OracleCommand())
            {
    cmd.Connection = conn;
    cmd.BindByName = true;
    cmd.CommandText = "return_table";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("\"v_ret\"", OracleDbType.Object, DBNull.Value, ParameterDirection.ReturnValue);
    conn.Open();
    try
    {
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

但这只会引发错误:

Invalid parameter binding
Parameter name: "v_ret"

我尝试了其他方法,但到目前为止没有任何效果。

我希望有一种方法可以在我的 C# 代码中解决这个问题,只是因为有很多我无法编辑的现有函数。

我也看过与我类似的问题,但无法从他们那里得到很好的答案。

【问题讨论】:

    标签: c# oracle oraclecommand


    【解决方案1】:

    这段 C# 代码可能对你有用,它适用于你自己定义的 Oracle 类型:

    using (OracleCommand cmd = new OracleCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "select * from table(return_table())";
        cmd.CommandType = CommandType.Text;
        conn.Open();
        OracleDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine(rdr.GetOracleDecimal(0));
            Console.WriteLine(rdr.GetOracleString(1));
        }
        conn.Close();
    }
    

    【讨论】:

      【解决方案2】:

      我认为您不能将用户定义的对象返回给 Oracle 数据提供程序。但是你可以返回一个 RefCursor:

      create or replace
      function return_table return SYS_REFCURSOR AS
      
      v_ret   t_nested_table;
      res SYS_REFCURSOR;
      
      begin
        v_ret  := t_nested_table();
      
        v_ret.extend;
        v_ret(v_ret.count) := t_col(1, 'one');
      
        v_ret.extend;
        v_ret(v_ret.count) := t_col(2, 'two');
      
        v_ret.extend;
        v_ret(v_ret.count) := t_col(3, 'three');
      
        OPEN res for SELECT * from TABLE(v_ret);
        RETURN res;
      end return_table;
      

      然后在 C# 中会是这样的:

      cmd.Parameters.Add("v_ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
      

      【讨论】:

        猜你喜欢
        • 2012-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-01
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多