How to call an Oracle function with a Ref Cursor as Out-parameter from C#? How to call an Oracle function with a Ref Cursor as Out-parameter from C#? oracle oracle

How to call an Oracle function with a Ref Cursor as Out-parameter from C#?


You sure can. There are a few gotchas to be wary of but here is a test case

create or replace function testodpRefCursor(                  uniqueId    IN NUMBER                  ,resultItems OUT NOCOPY SYS_REFCURSOR) RETURN NUMBER                 IS BEGIN      OPEN resultItems for select level from dual  connect by level < uniqueId ;      return 1; END testodpRefCursor;
  1. I have found thatfunctions likes to have theReturnValue as THE FIRST paramin the collection
  2. BindByName is by default FALSE, so it defaults to BIND BY POSITION

Otherwise it is quite straight forward:

  OracleCommand cmd = new OracleCommand("TESTODPREFCURSOR", con);  cmd.CommandType   = CommandType.StoredProcedure;  cmd.BindByName = true;  // Bind   OracleParameter oparam = cmd.Parameters.Add("ReturnValue", OracleDbType.Int64);  oparam.Direction = ParameterDirection.ReturnValue ;         OracleParameter oparam0 = cmd.Parameters.Add("uniqueId", OracleDbType.Int64);  oparam0.Value = 5 ;  oparam0.Direction = ParameterDirection.Input;  OracleParameter oparam1 = cmd.Parameters.Add("resultItems", OracleDbType.RefCursor);  oparam1.Direction = ParameterDirection.Output;  // Execute command  OracleDataReader reader;  try  {    reader = cmd.ExecuteReader();    while(reader.Read() ){        Console.WriteLine("level: {0}", reader.GetDecimal(0));      }  } ...

Now for more samples go to your Oracle Home directory and look @ the Ref cursor samples in ODP.NET

for instance:%oracle client home%\odp.net\samples\4\RefCursor

hth