1. 定义ORACLE 带输出游标的存储过程, 输出游标定义类型为:sys_refcursor
1 CREATE OR REPLACE PROCEDURE sp_demo (
2 emp_no IN VARCHAR2,
3 o_res1 OUT VARCHAR2,
4 o_res2 OUT VARCHAR2,
5 o_ds1 OUT sys_refcursor,
6 o_ds2 OUT sys_refcursor
7 )
8 IS
9 v_count NUMBER;
10 BEGIN
11 OPEN o_ds1 FOR
12 SELECT *
13 FROM table1;
14
15 OPEN o_ds2 FOR
16 SELECT *
17 FROM table2;
18
19 o_res1 := 'ok';
20 o_res2 := TO_CHAR (SYSDATE, 'yyyy/mm/dd hh24:mi:ss');
21 RETURN;
22 END;
2 emp_no IN VARCHAR2,
3 o_res1 OUT VARCHAR2,
4 o_res2 OUT VARCHAR2,
5 o_ds1 OUT sys_refcursor,
6 o_ds2 OUT sys_refcursor
7 )
8 IS
9 v_count NUMBER;
10 BEGIN
11 OPEN o_ds1 FOR
12 SELECT *
13 FROM table1;
14
15 OPEN o_ds2 FOR
16 SELECT *
17 FROM table2;
18
19 o_res1 := 'ok';
20 o_res2 := TO_CHAR (SYSDATE, 'yyyy/mm/dd hh24:mi:ss');
21 RETURN;
22 END;
2. C#调用
参数准备(petshop)
1 private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans,
2 CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
3 {
4 if (conn.State != ConnectionState.Open)
5 conn.Open();
6
7 cmd.Connection = conn;
8 cmd.CommandText = cmdText;
9 cmd.CommandType = cmdType;
10
11 if (trans != null)
12 cmd.Transaction = trans;
13
14 if (commandParameters != null)
15 {
16 foreach (OracleParameter parm in commandParameters)
17 cmd.Parameters.Add(parm);
18 }
19 }
2 CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
3 {
4 if (conn.State != ConnectionState.Open)
5 conn.Open();
6
7 cmd.Connection = conn;
8 cmd.CommandText = cmdText;
9 cmd.CommandType = cmdType;
10
11 if (trans != null)
12 cmd.Transaction = trans;
13
14 if (commandParameters != null)
15 {
16 foreach (OracleParameter parm in commandParameters)
17 cmd.Parameters.Add(parm);
18 }
19 }
//准备参数
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//获得所有输出游标,每个游标输出到ds的一个表中
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
//只获得第一个数据游标
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(rdr);
rdr.Close();
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//获得所有输出游标,每个游标输出到ds的一个表中
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
//只获得第一个数据游标
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(rdr);
rdr.Close();