oracle的存储过程返回记录集,关键之处是要用游标。 关于数据库的游标(cursor)大家肯定都接触不少,我们可以通过open,fetch,close操作控制游标进行各种方便的操作,这方面的例子我就不在重复了。我们现在要介绍的是游标变量(cursor variable)。类似游标,游标变量也是指向一个查询结果集的当前行。不同的是,游标变量能为任何类型相似(type-compatible)的查询打开,而并不是绑定到某一个特定的查询。通过游标变量,你可以在数据库的数据提取中获得更多的方便。
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(); } } |
闽公网安备 35060202000074号