如何从.NET中的存储过程返回oracle输出参数

Jes*_*ora 17 .net c# oracle

我在尝试从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);
Run Code Online (Sandbox Code Playgroud)

但它没有向变量sucess或errorMessage返回任何内容.我究竟做错了什么?有没有更好的办法?直接在Oracle上执行它可以正常工作.

uow*_*d01 18

看来你不能使用现有的变量作为输出参数,而是尝试这种方式

ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32).Direction = ParameterDirection.Output;

ora_cmd.ExecuteNonQuery();

if (ora_cmd.Parameters["Lc_Exito"].value == 0)
Run Code Online (Sandbox Code Playgroud)


vap*_*guy 5

我在任何地方都找不到可以记录整个过程的地方,因此在撞到墙头撞了撞之后,这是我想到的版本,使用了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
Run Code Online (Sandbox Code Playgroud)

然后,需要设置存储过程以接受OUT参数,并且您必须在过程中为其分配参数:

create or replace procedure PR_ABC_P_ALTA_TARJETA_PAYWARE(Lc_Exito OUT number)
  as
  begin
    Lc_Exito := 123;
  end;
 /
Run Code Online (Sandbox Code Playgroud)

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

  • 我想强调一下,“ Size”属性对于字符串输出参数非常重要,正如代码中的内联注释所示。 (3认同)