简体   繁体   English

如何在C#中获取oracle存储过程标准输出

[英]how to get oracle stored procedure standard output in c#

I am trying to get the stand output in c# returned from oracle stored procedure. 我正在尝试从oracle存储过程返回的c#中获得标准输出。 dbms_output.put_line('Hello Word') The c# code i am using is dbms_output.put_line('Hello Word')我正在使用的C#代码是

using (OracleConnection con = new OracleConnection())
{
    con.ConnectionString = My_connection_string;               
    con.Open();
    OracleCommand cmd = new OracleCommand("tmp_test", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.BindByName = true;
    var result = cmd.ExecuteScalar();
} 

The oracle stored procedure code is oracle存储过程的代码是

create or replace procedure tmp_test as
v_count integer;
begin
dbms_output.put_line('Hello Word');
end; 

The stored procedure execute successfully but I can't get the Hello Word back. 存储过程成功执行,但是我无法找回Hello Word。

After some struggle i have managed to find the answer and decided to post that might help other. 经过一番努力,我设法找到了答案,并决定发布可能对其他人有所帮助的内容。

using (OracleConnection con = new OracleConnection())
{
    con.ConnectionString = My_connection_string;               
    con.Open();
    OracleCommand cmd = new OracleCommand("tmp_test", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.BindByName = true;
    var result = cmd.ExecuteScalar();

    //it is included dbms_output
    cmd.CommandText = "begin dbms_output.enable (1000); end;";
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();

    string out_string;
    int status = 0;
    cmd.CommandText = "BEGIN DBMS_OUTPUT.GET_LINE (:out_string, :status); END;";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Clear();
    cmd.Parameters.Add("out_string", OracleType.VarChar, 32000);
    cmd.Parameters.Add("status", OracleType.Double);
    cmd.Parameters[0].Direction = System.Data.ParameterDirection.Output;
    cmd.Parameters[1].Direction = System.Data.ParameterDirection.Output;
    cmd.ExecuteNonQuery();
    out_string = cmd.Parameters[0].Value.ToString();
    status = int.Parse(cmd.Parameters[1].Value.ToString());
}

Here's a link to a the documentation for DBMS_OUTPUT package in Oracle - DBMS_OUTPUT 这是指向Oracle中DBMS_OUTPUT软件包的文档的链接-DBMS_OUTPUT

It specifically states that it's used for debugging Oracle Stored Procedures and that this is in essence a debug buffer that you need to actively poll use GET_LINES in order to see the output. 它特别指出它用于调试Oracle存储过程,并且从本质上讲,这是一个调试缓冲区,您需要主动轮询使用GET_LINES才能查看输出。

Function in PL/SQL should be something like this: PL / SQL中的功能应如下所示:

create or replace function tmp_test as
begin
   RETURN 'Hello World';
end; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM