【问题标题】:How to get the dbms.output value returned by a PL-SQL block in C#如何在 C# 中获取 PL-SQL 块返回的 dbms.output 值
【发布时间】:2012-03-21 16:04:52
【问题描述】:

我正在尝试使用System.Data.OrcaleClient 在 c# 中执行一个 PL-SQL 块。 PL-SQL 块在 oracle 中执行时,使用dbms.ouput 打印结果。

我想在我的 C# 代码中得到那个“dbms.ouput”结果。

请帮忙。

【问题讨论】:

  • 你为什么不把你的block变成function然后返回结果呢?
  • 嗨 Ben,我们正在与第三方集成,他们为我们提供了 PL-SQL 块,以获得所需的结果

标签: c# oracle plsql database dbms-output


【解决方案1】:

尝试使用dbms_output包的get_line函数。您可以创建一个过程来返回输出。像这样的东西(只是一个例子):

procedure call_with_output(p_output out varchar2) is
  vret integer := 0;
  vtxt varchar2(4000);
begin
  dbms_output.enable;
  -- here call code that generate lines
  -- use the loop to retrieve info
  while vret = 0 loop
    dbms_output.get_line(vtxt, vret);
    if vret = 0 then
      if p_output  is null then
        p_output := vtxt;
      else
       p_output := p_output || chr(10) || vtxt;
      end if;
    end if;
  end loop;
  dbms_output.disable;
end;

【讨论】:

  • 我喜欢你的解决方案。它结合了使用get_line(而不是get_lines)的简单性和对服务器的一次调用。我正在研究的答案涉及通过 ODP.NET 调用 get_lines,这是一个荒谬的问题。 +1
  • +1 但是请注意,这仅在输出总量为 32k 或更少时才有效。可能是这种情况,但如果您在循环中生成它,您可以获得相当数量的调试输出写入dbms_output
  • 要得到32K,vtxt的声明必须改成vtxt varcahr2(32767)
  • @Sérgio Michels - 这是一个不错的解决方案,但 pl-sql 块返回的数据很大。因此,由于使用 get_line 有大小限制,我认为它不起作用:(
  • @Dinu 尝试修改示例以插入到临时表中,而不是作为过程外返回。
【解决方案2】:

我正在使用下一个方法:

    private string GetDbmsOutputLine()
    {
        OracleCommand command = new OracleCommand
        {
            Connection = <connection>,
            CommandText = "begin dbms_output.get_line(:line, :status); end;",
            CommandType = CommandType.Text
        };

        OracleParameter lineParameter = new OracleParameter("line",  
            OracleType.VarChar);
        lineParameter.Size = 32000;
        lineParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(lineParameter);

        OracleParameter statusParameter = new OracleParameter("status",  
            OracleType.Int32);
        statusParameter.Direction = ParameterDirection.Output;
        command.Parameters.Add(statusParameter);

        command.ExecuteNonQuery();

        if (command.Parameters["line"].Value is DBNull)
            return null;

        string line = command.Parameters["line"].Value as string;

        return line;
    }

多次调用以获得多字符串值,因为使用 System.Data.OracleClient 调用 dbms_output.get_lines 存在问题。

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2017-11-04
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多