Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
How to fetch records using C#.Net and ODBC from a Oracle Stored Procedure which has Output parameters as cursor?

I have a stored procedure sp_myproc which has a cursor as a ouput parameters.

In my application , I'm using ODBC for connection to Oracle.
The Problem is ,there is no such type of odbctype for cursor .

so, How can I get records?

C#
OdbcConnection odbcCon = new OdbcConnection(connectionString);
OdbcCommand odbcCmd = new OdbcCommand();
odbcCmd.CommandText = "SP_myOracle";
odbcCmd.CommandType = CommandType.StoredProcedure;
//here I want to Add parameters as Cursor and Directon as Output

//
 DataTable dt = new DataTable();
 OdbcDataAdapter adap = new OdbcDataAdapter(odbcCmd);
 adap.Fill(dt);


my Stored Procedure is:

SQL
create or replace
PROCEDURE SP_myOracle
(EmpDETAILSCURSOR OUT  SYS_REFCURSOR)
AS
BEGIN
 OPEN EmpDETAILSCURSOR for 
Select EMP_NAME from Emp;
END


Thanks.
Posted
Updated 3-Sep-12 1:34am
v2

 
Share this answer
 
C#
static void Main(string[] args)
{
  string constr = "User Id=hr; Password=hr;
    Data Source=oramag; Pooling=false";

  OracleConnection con = new OracleConnection(constr);
  con.Open();

  OracleCommand cmd = con.CreateCommand();

  cmd.CommandText = "begin open :1 for
    select * from employees
    where manager_id=101; end;";

  OracleParameter p_rc = cmd.Parameters.Add(
    "p_rc",
    OracleDbType.RefCursor,
    DBNull.Value,
    ParameterDirection.Output);

  cmd.ExecuteNonQuery();

  cmd.Parameters.Clear();

  cmd.CommandText = "cursor_in_out.process_cursor";
  
  cmd.CommandType = CommandType.StoredProcedure;

  OracleParameter p_input = cmd.Parameters.Add(
    "p_input",
    OracleDbType.RefCursor,
    p_rc.Value,
    ParameterDirection.Input);

  cmd.ExecuteNonQuery();

  p_input.Dispose();
  p_rc.Dispose();
  cmd.Dispose();
  con.Dispose();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900