How to return oracle output parameters from a stored procedure in .NET

后端 未结 2 963
孤城傲影
孤城傲影 2020-12-01 14:39

I am having serious issues trying to get the data back from the SP. I was trying to do it like this:

OracleCommand ora_cmd = new OracleCommand(\"a6r1.PR_ABC         


        
相关标签:
2条回答
  • 2020-12-01 15:06

    I have not found anywhere where it documents the whole process in one place, so after hitting my head against the wall and banging it out, here is my version of what I came up with, using one of the output parameters from the OP's code:

    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
    

    Then the stored procedure needs to be set up to accept the OUT parameter and you must assign to it in the procedure:

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

    Obviously this leaves out all the other parameters that were being sent in and the other "out" parameters - wanted to simplify it. But this shows how everything gets set up, from before, during, and after the call to the stored procedure in the C#, and how to set the OUT parameter, and get the value out, of the stored procedure.

    0 讨论(0)
  • 2020-12-01 15:07

    It seems you cannot use existing variable as output parameter, try this way instead

    ora_cmd.Parameters.Add("Lc_Exito", OracleDbType.Int32).Direction = ParameterDirection.Output;
    
    ora_cmd.ExecuteNonQuery();
    
    if (ora_cmd.Parameters["Lc_Exito"].value == 0)
    
    0 讨论(0)
提交回复
热议问题