c#中的oracle输出参数问题
Posted
技术标签:
【中文标题】c#中的oracle输出参数问题【英文标题】:issue with oracle output parameters in c# 【发布时间】:2013-06-11 11:48:14 【问题描述】:我对甲骨文很生气。我一直在努力从 oracle 数据库中获取两个值(输出参数)。我需要根据 paymentType 和 vendor 获取两个值(o_unallocated_ledgercode 和 o_allocated_ledgercode)。
问题是:这些值在 C# 中仍然为空。我无法从数据库中获取这些值。可能有什么问题?请看我的 c# 代码。我需要别人的不同观点。我可能错过了什么。请给我代码示例。谢谢!
Oracle 包:
CREATE OR REPLACE package body BANKIMPORTS.pkg_vendor_config as
PROCEDURE get_ledger_codes (i_vendor in varchar2,
i_payment_type in varchar2,
o_allocated_ledgercode out varchar2,
o_unallocated_ledgercode out varchar2) is
BEGIN
IF UPPER (i_payment_type) = 'SUBS' THEN
SELECT CV.CONFIGVALUE
INTO o_unallocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Unallocated_LederCode'
AND V.DESCRIPTION = i_vendor;
SELECT CV.CONFIGVALUE
INTO o_allocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Payment_LedgerCode'
AND V.DESCRIPTION = i_vendor;
ELSIF UPPER (i_payment_type) = 'GOTV'
........same select statement as above
C# 代码:
using (DbCommand command = connection.CreateCommand())
try
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "pkg_vendor_config.get_ledger_codes";
command.AddParameter("i_vendor", DbType.String, vendor, ParameterDirection.Input);
command.AddParameter("i_payment_type", DbType.String, paymentType, ParameterDirection.Input);
command.AddParameter("o_unallocated_ledgercode", DbType.String, ParameterDirection.Output);
command.AddParameter("o_allocated_ledgercode", DbType.String, ParameterDirection.Output);
command.ExecuteNonQuery();
var unallocated = (String)command.Parameters["o_unallocated_ledgercode"].Value;
var allocated = (String)command.Parameters["o_allocated_ledgercode"].Value;
【问题讨论】:
您的意思是在您的C#
代码中将o_unallocated_ledgercode
放在o_allocated_ledgercode
之前吗?在您的 Oracle 过程中,顺序是相反的。此外,您是否从“输出”窗口收到任何错误或消息?
不,我需要从过程中获取 C# 中的两个值(o_unallocated_ledgercode 和 o_allocated_ledgercode)。 “顺序颠倒”是什么意思?
您可能希望在调用过程后添加检查以确保返回的 SQLCODE 值为零。分享和享受。
【参考方案1】:
tl;dr;我想你只需要设置参数的大小属性。
根据 docs.oracle.com 的page on the OracleParameter class object,当我们单击 Size 属性时,我们会看到参数类型的默认最大大小为 0!
当我设置所有 Varchar2 参数时,我不再获得空值。我已将它们设置为 50,但这将取决于您的 VENDOR_CONFIG_ITEM_VALUE.CONFIGVALUE 属性的限制设置为。
C#代码:
using (DbCommand command = connection.CreateCommand())
connection.Open();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "pkg_vendor_config.get_ledger_codes";
OracleParameter[] yourParams = new[]
new OracleParameter("i_vendor", OracleDbType.Varchar2, "subs", ParameterDirection.Input),
new OracleParameter("i_payment_type", OracleDbType.Varchar2, "cold hard cash", ParameterDirection.Input),
new OracleParameter("o_allocated_ledgercode", OracleDbType.Varchar2, ParameterDirection.Output),
new OracleParameter("o_unallocated_ledgercode", OracleDbType.Varchar2, ParameterDirection.Output)
;
foreach(var param in yourParams)
param.Size = 50;
command.Parameters.AddRange (yourParams);
command.ExecuteNonQuery();
var unallocated = (String)command.Parameters["o_unallocated_ledgercode"].Value.ToString();
var allocated = (String)command.Parameters["o_allocated_ledgercode"].Value.ToString();
connection.Close();
Oracle 包:
CREATE OR REPLACE Procedure pkg_vendor_config.get_ledger_codes(i_vendor in varchar2,
i_payment_type in varchar2,
o_allocated_ledgercode out varchar2,
o_unallocated_ledgercode out varchar2) is
BEGIN
SELECT 'Another value'
INTO o_unallocated_ledgercode
FROM dual;
SELECT 'Some value'
INTO o_allocated_ledgercode
FROM dual;
/* I'll assume this all behaved for you
IF UPPER (i_payment_type) = 'SUBS' THEN
SELECT CV.CONFIGVALUE
INTO o_unallocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Unallocated_LederCode'
AND V.DESCRIPTION = i_vendor;
SELECT CV.CONFIGVALUE
INTO o_allocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Payment_LedgerCode'
AND V.DESCRIPTION = i_vendor;
ELSIF UPPER (i_payment_type) = 'GOTV'
........same select statement as above
*/
end;
免责声明:
我似乎无法准确地重新创建您的 C# 设置;可能已经过时了,因为它已经快 6 年了。但这已确认有效
【讨论】:
【参考方案2】:您只是设置变量的值。如果你想返回一些东西使用函数。
FUNCTION get_ledger_codes
(
i_vendor in varchar2
, i_payment_type in varchar2
) RETURN VARCHAR2
is
o_allocated_ledgercode varchar2(100);
o_unallocated_ledgercode varchar2(100);
BEGIN
IF UPPER (i_payment_type) = 'SUBS' THEN
SELECT CV.CONFIGVALUE
INTO o_unallocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Unallocated_LederCode'
AND V.DESCRIPTION = i_vendor;
SELECT CV.CONFIGVALUE
INTO o_allocated_ledgercode
FROM VENDOR v
JOIN VENDOR_CONFIG_ITEM_VALUE CV ON v.Id = CV.Vendor_Id
JOIN CONFIG_ITEMS c ON CV.Config_Item_Id = c.Config_Item_Id
WHERE C.ITEMNAME = 'Subs_Payment_LedgerCode'
AND V.DESCRIPTION = i_vendor;
ELSIF UPPER (i_payment_type) = 'GOTV'
........same select statement as above
RETURN o_allocated_ledgercode || ',' || o_unallocated_ledgercode;
END;
【讨论】:
返回 o_allocated_ledgercode || ',' || o_unallocated_ledgercode;应该在if语句的末尾?你认为我的 c# 代码正确吗? 在我按照您的建议进行更改后,这些值在 c# 中仍然保持为空。请查看我的 c# 代码。非常感谢 我有一段时间没有做C#了。唯一看起来新的是 (String)command.Parameters 可以替换为:command.Parameters[i].ToString()?以上是关于c#中的oracle输出参数问题的主要内容,如果未能解决你的问题,请参考以下文章
C# 怎麼调用oracle procedure 中的number输入参数,