Oracle编号OUT参数始终为0

时间:2017-12-13 09:22:43

标签: c# oracle

我有一个存储过程,它接受几个输入参数和两个数字输出参数。如果我从sql developer执行存储过程,它会输出两个输出参数的正确值。

如果我从C#调用存储过程,则两个输出参数始终为0

我在这里展示代码:

    public int ExecuteStore(string storeToExecute, CountDocumentsData storeResultDataObject, int old_systemid, int current_systemid, params SearchFilter[] searchFilters)
    {
        using (OracleConnection connection = new OracleConnection(this.ConnectionString))
        {
            try
            {
                OracleCommand objCmd = new OracleCommand();
                objCmd.Connection = connection;
                connection.Open();
                objCmd.CommandText = storeToExecute;
                objCmd.CommandType = CommandType.StoredProcedure;

                foreach (var filter in searchFilters)
                {
                    OracleDbType type = OracleDbType.Varchar2;

                    if (filter.GetType() == typeof(YearFilter) || filter.GetType() == typeof(MonthFilter)
                            || filter.GetType() == typeof(ApplicationFilter) || filter.GetType() == typeof(DocumentTypeFilter))
                    {
                        type = OracleDbType.Varchar2;
                        objCmd.Parameters.Add(filter.FilterName, type, 200).Value = filter.Value;
                    }
                    else
                    {
                        type = OracleDbType.Int32;
                        objCmd.Parameters.Add(filter.FilterName, type).Value = filter.Value;
                    }                       
                }

                objCmd.Parameters.Add("Lower_SystemID", OracleDbType.Int32, 200).Value = old_systemid;
                objCmd.Parameters.Add("Higher_SystemID", OracleDbType.Int32, 200).Value = current_systemid;

                objCmd.Parameters.Add("DocumentsNumber", OracleDbType.Int32).Direction = ParameterDirection.Output;
                objCmd.Parameters.Add("DocumentsSize", OracleDbType.Int32).Direction = ParameterDirection.Output;

                int res = objCmd.ExecuteNonQuery();
                Console.WriteLine(Convert.ToDecimal(objCmd.Parameters["DocumentsNumber"].Value.ToString()));
                Console.WriteLine(Convert.ToDecimal(objCmd.Parameters["DocumentsSize"].Value.ToString()));

                return res;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Query error.", ex);
                throw new Exception("Error", ex);
            }
        }
    }

存储过程:

Procedure CountDocuments
(
    Application IN VARCHAR2, 

    YearDoc IN VARCHAR2,  

    MonthDoc IN VARCHAR2,

    UoId IN Integer,

    SearchOnSubjected IN Integer,

    DocType IN VARCHAR2,

    Lower_SystemID IN Integer,

    Higher_SystemID IN Integer,

    DocumentsNumber OUT Integer,

    DocumentsSize OUT Integer
  ) Is

  queryToExecute VarChar2 (2000);
  Begin

  queryToExecute := ...

  Execute Immediate queryToExecute 
             Into DocumentsNumber, DocumentsSize 
             Using YearDoc, MonthDoc, Lower_SystemID, Higher_SystemID;

  End CountDocuments;

1 个答案:

答案 0 :(得分:1)

您似乎使用动态参数,但您可以按位置绑定它们。这看起来很奇怪。我不知道运行时searchFilters中的内容是否与您的存储过程完全匹配,但如果不是,则需要使用

objCmd.BindByName = true;

或您的参数最终会出错。