使用 OracleCommand 获取 oracle 输出参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9494794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:38:51  来源:igfitidea点击:

Get oracle output parameter using OracleCommand

asp.netoraclesystem.data.oracleclient

提问by smilu

I have a oracle stored procedure which will return a value. I need to get the OUTPUT value in my C# program. I need to know how we can get the OUTPUT parameter using the OracleCommands AddWithValue method.

我有一个 oracle 存储过程,它将返回一个值。我需要在我的 C# 程序中获取 OUTPUT 值。我需要知道如何使用 OracleCommands AddWithValue 方法获取 OUTPUT 参数。

The way i have written now is:

我现在写的方式是:

 OracleCommand Ocmd = new OracleCommand(_StoredProcedure, OraCon);
    Ocmd.CommandType = CommandType.StoredProcedure;


            Ocmd.Parameters.AddWithValue("Filed1", "Value1");

            Ocmd.Parameters.AddWithValue("OUTPUTParam","").Direction = ParameterDirection.Output;

    OraCon.Open();
    int RecivedDetID = Ocmd.ExecuteNonQuery();
    OraCon.Close();

    return Ocmd.Parameters[_OutParam].Value.ToString();

I know the OUTPUTPARAm how i have called is wrong. How can i achieve it using the AddWithValue method of the OracleCommand. I dont want to use the OracleCommands Add method where we need to specify the Type also.

我知道我调用的 OUTPUTPARAm 是错误的。我如何使用 OracleCommand 的 AddWithValue 方法实现它。我不想在我们还需要指定类型的地方使用 OracleCommands Add 方法。

回答by Developer

Make sure you set the SIZE property on the parameter before executing. With output parameters in Oracle, the specified size acts as a buffer. If the buffer isn't set, it is 0 so you don't get the value from the database.

确保在执行之前在参数上设置 SIZE 属性。对于 Oracle 中的输出参数,指定的大小充当缓冲区。如果未设置缓冲区,则它为 0,因此您无法从数据库中获取该值。

var param = Ocmd.Parameters.AddWithValue("OUTPUTParam","").Direction = ParameterDirection.Output;
param.Size = 255;

The rest is good!

其余都不错!