【问题标题】:How to return oracle output parameters from a stored procedure in .NET如何从 .NET 中的存储过程返回 oracle 输出参数
【发布时间】:2012-12-24 06:00:05
【问题描述】:

我在尝试从 SP 取回数据时遇到严重问题。我试图这样做:

OracleCommand ora_cmd = new OracleCommand("a6r1.PR_ABC_P_ALTA_TARJETA_PAYWARE", ora_conn);
                    ora_cmd.BindByName = true;
                    ora_cmd.CommandType = CommandType.StoredProcedure;

                    int success= new int();

                    ora_cmd.Parameters.Add("Lc_Param_Issuer", OracleDbType.Varchar2, issuer, ParameterDirection.Input);
                    ora_cmd.Parameters.Add("Ln_Param_Valid_Product", OracleDbType.Varchar2, DropDownListProducto.SelectedValue.ToString(), ParameterDirection.Input);
                    ora_cmd.Parameters.Add("Ln_Param_Total", OracleDbType.Int32, parsed, ParameterDirection.Input);
                    ora_cmd.Parameters.Add("Lc_Param_User", OracleDbType.Varchar2, user, ParameterDirection.Input);
                    ora_cmd.Parameters.Add("Lc_Encrypted_Password", OracleDbType.Varchar2, pass, ParameterDirection.Input);
                    ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32, success, ParameterDirection.Output);
                    ora_cmd.Parameters.Add("Lc_Error", OracleDbType.Varchar2, errorMessage, ParameterDirection.Output);

但它没有向变量 sucess 或 errorMessage 返回任何内容。我究竟做错了什么?有没有更好的办法?直接在 Oracle 上执行时效果很好。

【问题讨论】:

    标签: c# .net oracle


    【解决方案1】:

    您似乎不能使用现有变量作为输出参数,请尝试这种方式

    ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32).Direction = ParameterDirection.Output;
    
    ora_cmd.ExecuteNonQuery();
    
    if (ora_cmd.Parameters["Lc_Exito"].value == 0)
    

    【讨论】:

    • 是的,你完全正确。我没想到 oracle 参数与 sql 的参数相比如此具体。非常比你。
    • if (ora_cmd.Parameters["Lc_Exito"]).value == 0) 中有一个额外的括号。你的意思是if (ora_cmd.Parameters["Lc_Exito"].value == 0)
    【解决方案2】:

    我还没有找到将整个过程记录在一个地方的任何地方,所以在将我的头撞到墙上并将其敲出之后,这是我想出的版本,使用来自OP的代码:

    OracleParameter param = new OracleParameter();
    param = ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32, ParameterDirection.Output);  // can assign the direction within the parameter declaration
    param.Size = 25;  // failed for me if I did not have this - should be the same as the DB field, if exporting a value from the database
    
    ora_cmd.ExecuteNonQuery();
    
    int myLc_ExitoValue = int.Parse(param.Value);  // might not need to parse, and might need a .ToString() on param.Value if you do - I was using strings so not sure about OP's exact case
    

    那么存储过程需要设置为接受OUT参数并且你必须在过程中赋值给它:

    create or replace procedure PR_ABC_P_ALTA_TARJETA_PAYWARE(Lc_Exito OUT number)
      as
      begin
        Lc_Exito := 123;
      end;
     /
    

    显然,这忽略了所有其他正在发送的参数和其他“输出”参数 - 想要简化它。但这显示了从调用 C# 中的存储过程之前、期间和之后如何设置所有内容,以及如何设置 OUT 参数并获取存储过程的值。

    【讨论】:

    • 我想强调“Size”属性对于字符串输出参数非常重要,正如代码中的内联注释所示。
    猜你喜欢
    • 1970-01-01
    • 2022-08-07
    • 2015-08-18
    • 2018-07-28
    • 2017-02-21
    • 2019-07-12
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多