C# - Oracle存储过程返回没有行的RefCursor

时间:2014-05-27 13:23:28

标签: c# oracle stored-procedures ref-cursor odp.net-managed

我目前正在使用概念验证(WinForms)来测试对Oracle存储过程的调用。

环境:

  • Windows x64
  • .NET Framework:4.0
  • 安装了Oracle客户端(v11.2)但不是必需的
  • Oracle Data Provider .NET:Oracle.ManagedDataAccess(v121.1.2)

好吧,我确定我的存储过程必须返回7行。平台(x86或x64不是问题,因为它适用于两个平台)。

部分代码:

private void btnExecuteODP_Click(object sender, EventArgs e){
    string _connectionString = 
        String.Format("Data Source={0};User Id={1};Password={2};",
            tbDataSource.Text, tbUser.Text, tbPassword.Text);

try{
    using (OracleConnection conn = new OracleConnection(_connectionString)){
        using (OracleCommand cmd = new OracleCommand()){
            cmd.CommandText = "XXCOOT_QUERIES.getWarehouses";
            cmd.CommandType = CommandType.StoredProcedure;

            // Parameters
            OracleParameter p1 = new OracleParameter();
            p1.ParameterName = "inumActivityId";
            p1.OracleDbType = OracleDbType.Varchar2;
            p1.Direction = ParameterDirection.Input;
            p1.Size = 10;
            p1.Value = 108;
            cmd.Parameters.Add(p1);

            OracleParameter cursor = new OracleParameter();
            cursor.ParameterName = "ocursWarehouses";
            cursor.OracleDbType = OracleDbType.RefCursor;
            cursor.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(cursor);

            OracleParameter p2 = new OracleParameter();
            p2.ParameterName = "ostrErrCode";
            p2.OracleDbType = OracleDbType.Varchar2;
            p2.Direction = ParameterDirection.Output;
            p2.Size = 2000;
            cmd.Parameters.Add(p2);

            DataTable dt = new DataTable();
            OracleDataReader reader = null;

            conn.Open();
            cmd.Connection = conn;

            using (reader = cmd.ExecuteReader()){
                dt.Load(reader);
                this.tbResult.Text = "Rows Count : " +
                    dt.Rows.Count.ToString(); // Returns: "Rows Count : 0"

                this.dataGridView1.DataSource = dt.DefaultView;
                // Returns : " | Column1 | Column2 | "
                //             |-------- | --------|
            }
        }
    }
}
finally{}
}

我的存储过程:

PROCEDURE getWarehouses
     ( inumActivityId   in   varchar2
     , ocursWarehouses  out  grefCursorType
     , ostrErrCode      out  varchar2
 )
IS
    lextorg varchar2(150);
BEGIN
    FND_CLIENT_INFO.Set_Org_Context(inumActivityId);
   lextorg := null;

   SELECT b.GENERIC_EXTERNAL_ORGANIZATION
   INTO   lextorg
   FROM   HR_ORGANIZATION_INFORMATION a
   ,      HR_ORGANIZATION_INFORMATIO_DFV b
   WHERE  a.organization_id = to_number(inumActivityId)
   AND    a.ORG_INFORMATION_CONTEXT = 'Operating Unit Information'
   AND    a.rowid = b.rowid;

IF lextorg is null THEN

    OPEN ocursWarehouses FOR
        SELECT ood.organization_code  organization_code
         ,      ood.organization_name  name
         FROM   org_organization_definitions  ood
         ,      mtl_parameters                mp
         ,      mtl_parameters_dfv            mp_dfv
         WHERE ood.operating_unit = to_number(inumActivityId)
         AND   sysdate <= nvl(disable_date,sysdate)
         AND   ood.organization_id = mp.organization_id
         AND   mp.rowid = mp_dfv.row_id
         AND   upper(mp_dfv.warehouse_type) in ('INTERNAL','EXTERNAL')
         ORDER by organization_name;

ELSE

    OPEN ocursWarehouses FOR
        SELECT ood.organization_code  organization_code
        ,      ood.organization_name  name
        FROM   org_organization_definitions  ood
        ,      mtl_parameters                mp
        ,      mtl_parameters_dfv            mp_dfv
        WHERE -ood.operating_unit = to_number(inumActivityId)
        AND sysdate <= nvl(disable_date,sysdate)
        AND   ood.organization_id = mp.organization_id
        AND   mp.rowid = mp_dfv.row_id
        AND   upper(mp_dfv.warehouse_type) in ('INTERNAL','EXTERNAL')
        ORDER by organization_name;
END/

执行时:   没有警告,没有错误(所有参数都是有效的)

我的问题:   返回任何行(而不是7个预期的行)但我检索模式(列名称是更正的)。

==&GT;如果有人看到我的代码错在哪里?还是读书?

2 个答案:

答案 0 :(得分:0)

好吧,经过几个月和几个月,我找到了问题的原因:

我的存储过程在其FROM子句中使用视图。这个(这些)视图使用 LOB数据类型(由于Oracle返回错误,我发现了这个问题)。

答案 1 :(得分:0)

同样的事情发生在我身上,我的问题是我的连接字符串,我没有打开连接......