C 執行oracle返回游標型別的儲存過程

2022-02-15 23:28:26 字數 3667 閱讀 8204

c#執行oracle儲存過程,儲存過程**為:

create

orreplace

procedure proc_test(pcursor out pak_pub.ut_cursor) as

begin

open pcursor for

select

*from

scott.emp;

end proc_tes;

其中pak_pub.ut_cursor的定義為:

create

orreplace

package pak_pub

astype ut_cursor

is ref cursor

;

end;

兩種方式:1、用微軟自帶的oracle資料訪問介面(using system.data.oracleclient)

注意.net 4.0裡已經沒有了system.data.oracleclient

2、使用odp.net(引用oracle客戶端裡的oracle.dataaccess.dll)

//

儲存過程引數設定

oracleparameter sqlparme = new oracleparameter[1

];

//引數1

sqlparme[0] = new oracleparameter("

pcursor

", oracletype.cursor);

sqlparme[

0].direction =parameterdirection.output;

datatable re = dbaccess.dbexecstoredprocedure("

proc_test

", sqlparme);

dbaccess.dbexecstoredprocedure函式實現 

public

static datatable dbexecstoredprocedure(string

spname, oracleparameter sqlparme)

dataset ds1;

datetime endtime;

timespan sp;

ds1 = new

dataset();

datetime begtime =system.datetime.now;

oracledataadapter da1 = new oracledataadapter(oracmd);//

取出資料

da1.fill(ds1);

endtime =system.datetime.now;

string str = "

spare time:

" + (endtime -begtime).tostring();

datatable dt = ds1.tables[0

];

//除錯

if (dt != null && dt.rows.count != 0

) messagebox.show(

"返回記錄數

" + dt.rows.count.tostring() + "

\n" +str);

tracelog.trace(

"執行時間

", "

返回記錄數

" + dt.rows.count.tostring() + "

\n" +str);

return

dt;

} catch

(invalidcastexception e)

}

第二種方式:

//

使用oracle客戶端訪問介面

oracleparameter sqlparme = new oracleparameter[1

];

//引數1

sqlparme[0] = new oracleparameter("

pcursor

", oracledbtype.refcursor);

sqlparme[

0].direction =parameterdirection.output;

datatable re = dbaccess.dbexecstoredprocedure("

proc_test

", sqlparme);

dbaccess.dbexecstoredprocedure函式實現同第一種方式的.

資料庫連線(odp.net方式)

//

db連線

public

static

bool dbconnect(string puser,string pwd,string

ds)

catch

}

是用.net自帶的訪問介面連線方法稍有不同:

(連線字串應該這樣寫)

stringconstring ="data source=test;user=test001;password=123456";

感覺上其實區別不大,只是如果要用到pl/sql的陣列變數的時候,第二種方式的特點就突顯出來了,(而且微軟已經宣告.net 4.0後不在包含oracle的訪問介面,所以很多開發者都開始轉向使用oracle公司提供的訪問介面)

oracle客戶端的資料訪問介面支援param1.collectiontype = oraclecollectiontype.plsqlassociativearray,陣列變數,而且支援長度限制,由於我演示的沒有傳入引數,這裡就簡單說一下,有傳入引數的時候,這樣

sqlparme[0] = new oracleparameter("

param1

", oracledbtype.varchar2,1000);//

可限制傳入引數長度

sqlparme[0].direction =parameterdirection.input;

string s = "

傳入引數

";

sqlparme[

0].value = s;

如果傳入引數是pl/sql的陣列型別那就這樣傳

sqlparme[0] = new oracleparameter("

pdimensionfields

", oracledbtype.varchar2,1000

);

sqlparme[

0].direction =parameterdirection.input;

sqlparme[

0].collectiontype =oraclecollectiontype.plsqlassociativearray

string s =new

string;

sqlparme[

0].value = s;

C 執行oracle返回游標型別的儲存過程

c 執行oracle儲存過程,儲存過程 為 create or replace procedure proc test pcursor out pak pub.ut cursor as begin open pcursor for select from scott.emp end proc tes...

執行Oracle儲存過程返回游標結果集

create or replace package returncursor is type cur cj is ref cursor procedure find emp out cur cj end returncursor create or replace package body retu...

Oracle儲存過程返回游標

oracle儲存過程返回游標 有倆種方法 一種是宣告系統游標,一種是宣告自定義游標,然後後面操作一樣,引數型別為 in out 或out 1 宣告個人系統游標.推薦 create or replace p temp procedure cur arg out sys refcursor 方法1 be...