简体   繁体   English

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

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

I am having serious issues trying to get the data back from the SP. 我在尝试从SP取回数据时遇到严重问题。 I was trying to do it like this: 我正在尝试这样做:

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);

But it is not returning anything to the variables sucess or errorMessage. 但是它没有向变量sucess或errorMessage返回任何内容。 What am I doing wrong? 我究竟做错了什么? Is there a better way? 有没有更好的办法? It works fine when executed directly on Oracle. 直接在Oracle上执行时,它可以正常工作。

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)

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: 我在任何地方都找不到可以记录整个过程的任何地方,因此在撞到墙头撞了撞之后,这是我想出的内容的版本,使用了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

Then the stored procedure needs to be set up to accept the OUT parameter and you must assign to it in the procedure: 然后,需要将存储过程设置为接受OUT参数,并且您必须在该过程中为其分配:

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. 但这显示了如何在C#中调用存储过程之前,之中和之后设置所有内容,以及如何设置OUT参数并从存储过程中获取值。

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

相关问题 如何使用 Oracle 中的用户定义数据类型参数从 .NET 前端执行 Oracle 存储过程 - How to execute Oracle stored procedure with User Defined DataTypes Parameters in Oracle from .NET Front-End 将带有存储过程的参数从asp.net传递到oracle - Passing parameters with a stored procedure to oracle from asp.net 如何从C#函数的存储过程中返回多个输出参数 - How to return multiple output parameters from stored procedure for C# function 如何使用 IMultipleResults 并从 SQL 存储过程返回 Output 参数? - How can I use IMultipleResults AND return Output Parameters from a SQL Stored procedure? 具有输出/输入参数返回计数的存储过程 - Stored procedure with output / input parameters return count 如何使用ODP.NET从Oracle存储过程获取CLOB输出? - How to get CLOB output from Oracle Stored Procedure using ODP.NET? 如何从oracle存储过程中读取不同类型的out参数? - How to read different type of out parameters from oracle stored procedure? Oracle存储过程将多个结果集返回到.net - Oracle stored Procedure to return multiple resultsets to .net 如何在C#中从Oracle存储过程获取XML输出 - How to bring XML output from Oracle Stored procedure in c# 如何将表值参数从 .net 代码传递给存储过程 - How to pass table value parameters to stored procedure from .net code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM