oracle的存儲過程返回記錄集,關鍵之處是要用游標。關于數據庫的游標(cursor)大家肯定都接觸不少,我們可以通過open,fetch,close操作控制游標進行各種方便的操作,這方面的例子我就不在重復了。
我們現在要介紹的是游標變量(cursor variable)。類似游標,游標變量也是指向一個查詢結果集的當前行。不同的是,游標變量能為任何類型相似(type-compatible)的查詢打開,而并不是綁定到某一個特定的查詢。通過游標變量,你可以在數據庫的數據提取中獲得更多的方便。
首先是建立表:
create table lihuan.bill_points
(
points_id number(10,0) not null,
customer_id number(10,0) not null,
bill_point_no number(2,0) default 1 not null,
constraint pk_bill_points primary key (points_id)
)
/其次,建package
create or replace package lihuan.yy_pkg_bill_point_no/**//*取得用戶的所有計費電序號*/
is
type t_cursor is ref cursor;
procedure bill_point_no(p_customer_id bill_points.customer_id%type,
re_cursor out t_cursor);
end;
/再次,建package body
create or replace package body lihuan.yy_pkg_bill_point_no/**//*取得用戶的所有計費電序號*/
is
procedure bill_point_no(p_customer_id bill_points.customer_id%type,
re_cursor out t_cursor)
is
v_cursor t_cursor;
begin
open v_cursor for
select bill_point_no from bill_points where customer_id =p_customer_id;
re_cursor := v_cursor;
end;
end;
/最后,在.net中程序調用。
public dataset bill_point_no(string customer_id)//ok
{
dataset dataset = new dataset();
hashtable ht=new hashtable();
ht.add("p_customer_id",customer_id);
if(runprocedure("re_cursor",oracletype.cursor,ref dataset,
ht,bmsoracleuser+".yy_pkg_bill_point_no.bill_point_no",
bmsoracleconnectionstring))
{
;
}
else
{
dataset=null;
}
return dataset;
}
public bool runprocedure(string returnparameter,
oracletype paramtype,ref dataset dataset,hashtable ht ,
string procedurename,string oracleconnection)
{
system.data.oracleclient.oracleconnection dsconnection =
new system.data.oracleclient.oracleconnection(oracleconnection);
system.data.oracleclient.oraclecommand dacommand =
new system.data.oracleclient.oraclecommand(procedurename,dsconnection);
dsconnection.open();
dacommand.commandtype=commandtype.storedprocedure;
idictionaryenumerator enumerator;
enumerator = ht.getenumerator();
object value=null;
oracleparameter oracleparam;
oracleparam = dacommand.parameters.
add(new oracleparameter(returnparameter,paramtype));
oracleparam.direction = parameterdirection.output;
while(enumerator.movenext())
{
value = enumerator.value;
oracleparam=dacommand.parameters.
add(new oracleparameter
(enumerator.key.tostring(), value));
}
oracledataadapter odadapter=new oracledataadapter(dacommand);
try
{
odadapter.fill(dataset);
return true;
}
catch(system.exception e)
{
e.tostring();
return false;
}
finally
{
ht.clear();
dacommand.parameters.clear();
dsconnection.close();
}
},歡迎訪問網頁設計愛好者web開發。
新聞熱點
疑難解答