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. 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 }
//準備參數
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();