本系列文章导航
[Oracle]高效的PL/SQL程序设计(一)--伪列ROWNUM使用技巧
[Oracle]高效的PL/SQL程序设计(二)--标量子查询
[Oracle]高效的PL/SQL程序设计(三)--Package的优点
[Oracle]高效的PL/SQL程序设计(四)--批量处理
[Oracle]高效的PL/SQL程序设计(五)--调用存储过程返回结果集
[Oracle]高效的PL/SQL程序设计(六)--%ROWTYPE的使用
Oracle支持通过ref游标在调用存储过程后返回结果集,使用游标在内存消耗以及时间上都要大大的优于返回数组变量的做法!
示例如下:数据库方面,建立一个Package
create
or
replace
packageref_cur_demo
is
typerc is ref cursor ;
procedure ref_cursor(p_owner in varchar2 ,p_cursor in outrc);
end ref_cur_demo;
create or replace packagebodyref_cur_demo is
procedure ref_cursor(p_owner in varchar2 ,p_cursor in outrc)
is
begin
open p_cursor for select object_name ,object_type from all_objects where owner = p_owner and rownum < 3 ;
end ;
end ref_cur_demo;
typerc is ref cursor ;
procedure ref_cursor(p_owner in varchar2 ,p_cursor in outrc);
end ref_cur_demo;
create or replace packagebodyref_cur_demo is
procedure ref_cursor(p_owner in varchar2 ,p_cursor in outrc)
is
begin
open p_cursor for select object_name ,object_type from all_objects where owner = p_owner and rownum < 3 ;
end ;
end ref_cur_demo;
程序方面使用C#建立一个小型应用程序,主要代码如下:
Oracle.DataAccess.Client.OracleConnectionoracleConnection1
=
new
OracleConnection(
"
datasource=precolm2;userid=colmtest;password=colmtest
"
);
oracleConnection1.Open();
string strSQL = @" ref_cur_demo.ref_cursor " ;
Oracle.DataAccess.Client.OracleDataAdapterda = new Oracle.DataAccess.Client.OracleDataAdapter();
Oracle.DataAccess.Client.OracleCommandcmd = new Oracle.DataAccess.Client.OracleCommand(strSQL,oracleConnection1);
cmd.CommandType = CommandType.StoredProcedure;
Oracle.DataAccess.Client.OracleParameterpram = new Oracle.DataAccess.Client.OracleParameter( " p_owner " ,Oracle.DataAccess.Client.OracleDbType.Varchar2);
pram.Value = " COLMTEST " ;
cmd.Parameters.Add(pram);
Oracle.DataAccess.Client.OracleParameterpram1 = new Oracle.DataAccess.Client.OracleParameter( " p_cursor " ,Oracle.DataAccess.Client.OracleDbType.RefCursor);
pram1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pram1);
da.SelectCommand = cmd;
DataSetds = new DataSet();
da.Fill(ds);
this .dataGrid1.DataSource = ds.Tables[ 0 ].DefaultView;
oracleConnection1.Open();
string strSQL = @" ref_cur_demo.ref_cursor " ;
Oracle.DataAccess.Client.OracleDataAdapterda = new Oracle.DataAccess.Client.OracleDataAdapter();
Oracle.DataAccess.Client.OracleCommandcmd = new Oracle.DataAccess.Client.OracleCommand(strSQL,oracleConnection1);
cmd.CommandType = CommandType.StoredProcedure;
Oracle.DataAccess.Client.OracleParameterpram = new Oracle.DataAccess.Client.OracleParameter( " p_owner " ,Oracle.DataAccess.Client.OracleDbType.Varchar2);
pram.Value = " COLMTEST " ;
cmd.Parameters.Add(pram);
Oracle.DataAccess.Client.OracleParameterpram1 = new Oracle.DataAccess.Client.OracleParameter( " p_cursor " ,Oracle.DataAccess.Client.OracleDbType.RefCursor);
pram1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pram1);
da.SelectCommand = cmd;
DataSetds = new DataSet();
da.Fill(ds);
this .dataGrid1.DataSource = ds.Tables[ 0 ].DefaultView;
博文来源: http://blog.csdn.net/huanghui22/archive/2007/05/23/1622820.aspx