c#中的oracle输出参数问题

时间:2013-06-11 11:48:14

标签: c# oracle plsql output-parameter

我对甲骨文很生气。我一直在努力从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;

2 个答案:

答案 0 :(得分:0)

您只是设置变量的值。如果你想返回一些使用函数。

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;

答案 1 :(得分:0)

tl; dr; 我想您只需要设置参数的Size属性即可。

根据docs.oracle.com的page on the OracleParameter class object,当我们单击Size属性时,我们看到参数类型的默认最大大小为0!

当我设置了所有的Varchar2参数时,我不再获得null值。我已经将它们设置为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年以来,所有这些都可能过时了。但这已确认有效