C#调用Oracle存储过程并返回结果集

1.创建一个存储过程:

create or replace procedure GET_PERSON

(

    RC1 out sys_refcursor,

    RC2 out sys_refcursor

)

is

begin

    open RC1 for select * from PERSON;

    open RC2 for select * from PERSON;

end GET_PERSON;

 

2.C#代码

OracleConnection conn = new OracleConnection("Data Source=XXX;User Id=scott;Password=tiger");

OracleCommand cmd = new OracleCommand("GET_PERSON", conn);

cmd.CommandType = CommandType.StoredProcedure;

 

OracleParameter param = new OracleParameter("RC1", OracleType.Cursor);

param.Direction = ParameterDirection.Output;

cmd.Parameters.Add(param);

param = new OracleParameter("RC2", OracleType.Cursor);

param.Direction = ParameterDirection.Output;

cmd.Parameters.Add(param);

OracleDataAdapter adapter = new OracleDataAdapter(cmd);

 

DataSet ds = new DataSet();

adapter.Fill(ds);

 

OK!

 

如果是Oracle9之前的版本,需要定义包含REF CURSOR的包.

例如:

create or replace package GLOBAL_OBJECT is

    type RC_OBJECT is ref cursor;

end GLOBAL_OBJECT;

 

create or replace procedure GET_PERSON

(

    RC1 out RC_OBJECT ,

    RC2 out RC_OBJECT

)

is

begin

  open RC1 for select * from PERSON;

 

  open RC2 for select * from PERSON;

end GET_PERSON;

 

 

你可能感兴趣的:(数据库)