执行一个返回自定义Cursor的存储过程?

时间:2014-03-12 21:29:44

标签: c# oracle ado.net odp.net user-defined-types

给定自定义游标类型:

create or replace 
PACKAGE                               Types AS
  TYPE cursor_type IS REF CURSOR;
END Types;

返回此类型的存储过程:

create or replace 
procedure TheProcedure
(RS OUT someSchema.TYPES.CURSOR_type, someCode number)
is 
begin
open RS for 
    --A custom query
     select 
            someTable.field,
            anotherTable.field,

      from someTable,
           anotherTable,
       where 
           anotherTable.code = someTable.code
           AND someTable.someFK = someCode;
end TheProcedure;

我需要使用ODP.Net(Oracle.DataAccess.Client)

检索所有数据
string connectionString = ConfigurationManager.ConnectionStrings["cnx"].ConnectionString;
using (OracleConnection conn = new OracleConnection(connectionString))
{
    conn.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;

    cmd.CommandText = "ASCHEMA.TheProcedure";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    OracleParameter prmCursor = cmd.Parameters.Add("RS", OracleDbType.RefCursor);
    prmCursor.Direction = ParameterDirection.Output;
    cmd.Parameters.Add("someCode", OracleDbType.NVarchar2).Value = 19685;
    //the integrity of the sql query was verified
    cmd.ExecuteNonQuery();
    OracleDataReader anotherReader = ((OracleRefCursor)prmCursor.Value).GetDataReader();

    while (anotherReader.Read())
    {
        var some = !anotherReader.IsDBNull(0) ? anotherReader.GetValue(0) : 0;
    }
}

但是读者anotherReader永远不会返回数据,我认为我需要为用户定义类型TYPE.cursor_type做一些特殊的事情,但是如何?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您真的需要将其设为用户定义类型吗?只需使用SYS_REFCURSOR即可。以下是一些示例代码:

http://www.oracle.com/technetwork/articles/dotnet/williams-refcursors-092375.html