如何调用返回引用游标的Oracle存储过程

时间:2013-10-10 11:49:36

标签: c# asp.net-mvc-4 stored-procedures plsql oracle11g

我试图调用返回引用游标的Oracle存储过程,我需要从返回的数据生成树视图。我是新手,我有两个问题。

第一个问题是我无法调用该程序。我收到此错误:“调用'OBJECT_HIERARCHY'时参数的数量或类型错误”

我的第二个问题是,我不明白当这个过程返回一个引用游标值时,我将如何获取该数据?该表中有超过5000条记录,我没有获得该数据,而是一个引用游标值。有人可以解释我如何使用ref游标值获取该数据。我没有使用Oracle的经验。

这是oracle中的过程定义:

CREATE OR REPLACE PROCEDURE SAD.object_hierarchy  
(nAppId IN NUMBER,
nParentId IN NUMBER DEFAULT -1, 
o_cRefCursor OUT SYS_REFCURSOR)
IS
BEGIN
IF NOT o_cRefCursor%ISOPEN THEN

  OPEN o_cRefCursor FOR 
     SELECT
        h.PARENT_ID, h.CHILD_ID, h.H_LEVEL,
        o.OBJECT_IDENTIFIER, o.OBJECT_TYPE_ID
     FROM
     (
        SELECT
           PARENT_ID, CHILD_ID, LEVEL AS H_LEVEL
        FROM OBJECT_RELATIONSHIPS
        START WITH PARENT_ID = nParentId --> -1 --= 60170
        CONNECT BY PRIOR CHILD_ID = PARENT_ID
     ) h
     INNER JOIN
        OBJECTS o
        ON
           o.OBJECT_ID = h.CHILD_ID AND
           O.APPLICATION_ID = nAppId;   
 END IF;
END object_hierarchy;

这些是表字段定义

Column Name               Data Type            

 OBJECT_REL_ID            NUMBER (14)                    
 PARENT_ID                NUMBER (14)                    
 CHILD_ID                 NUMBER (14)                    
 OBJECT_IDENTIFIER        VARCHAR2 (255 Byte)    
 OBJECT_TYPE_ID           VARCHAR2 (5 Byte)

这是我的代码,它返回错误:

            string oradb = "Data Source=(DESCRIPTION="
         + "(ADDRESS=(PROTOCOL=TCP)(HOST=tnt33)(PORT=1521))"
         + "(CONNECT_DATA=(SERVICE_NAME=ORCL)));"
         + "User Id=xxx;Password=xxxxx;";
        OracleConnection con = new OracleConnection(oradb);

        try
        {
            con.Open();
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = con;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "SAD.object_hierarchy";
            cmd.Parameters.Add("nAppId", OracleDbType.Int16).Value = 1;
            OracleParameter oraP = new OracleParameter();
            oraP.OracleDbType = OracleDbType.RefCursor;
            oraP.Direction = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(oraP);
            OracleDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {

            }
            reader.Close();    
        }
        catch (Exception ex)
        {

            con.Close();
        }

有人可以帮助我并向我解释为什么我的代码会返回此错误:“调用'OBJECT_HIERARCHY'时参数的数量或类型错误”

2 个答案:

答案 0 :(得分:10)

示例:

string connStr = "Data Source=datasource;Persist Security Info=True;User ID=user;Password=pass;Unicode=True";
DataSet dataset = new DataSet();

string connStr = ConfigurationManager.ConnectionStrings["OracleConn"].ToString();

using (OracleConnection objConn = new OracleConnection(connStr))
{
    OracleCommand objCmd = new OracleCommand();
    objCmd.Connection = objConn;
    objCmd.CommandText = "Oracle_PkrName.Stored_Proc_Name";
    objCmd.CommandType = CommandType.StoredProcedure;
    objCmd.Parameters.Add("Emp_id", OracleType.Int32).Value = 3; // Input id
    objCmd.Parameters.Add("Emp_out", OracleType.Cursor).Direction = ParameterDirection.Output;

    try
    {
        objConn.Open();
        objCmd.ExecuteNonQuery();
        OracleDataAdapter da = new OracleDataAdapter(objCmd);
        da.Fill(dataset);                   
    }
    catch (Exception ex)
    {
        System.Console.WriteLine("Exception: {0}", ex.ToString());
    }
    objConn.Close();
}

答案 1 :(得分:4)

如果您要提供OUT,则还需要提供nParentId,因为.NET在将语句发送到服务器时不会命名这些参数。

cmd.Parameters.Add("nParentId", OracleDbType.Int16).Value = -1;