ASP.NET调用oracle存储过程实现快速分页
在WEB开发中,分页是我们必须要解决的一个问题,今天我们一起来看看ASP.NET调用ORACEL存储过程实现分页的方法。
包定义:
以下是引用片段:
createorreplacepackageMaterialManageis
TYPET_CURSORISREFCURSOR;
ProcedurePer_QuickPage
(
TbNameinvarchar2,--表名
FieldStrinvarchar2,--字段集
RowFilterinvarchar2,--过滤条件
SortStrinvarchar2,--排序集
RownumFieldStrinvarchar2,--分页条件
TotalCountoutnumber,--总记录数
Cur_ReturnCuroutT_CURSOR--返回的游标
);
endMaterialManage;
包主体:
以下是引用片段:
createorreplacepackagebodyMaterialManageis
ProcedurePer_QuickPage
(
TbNameinvarchar2,--表名
FieldStrinvarchar2,--字段集
RowFilterinvarchar2,--过滤条件
SortStrinvarchar2,--排序集
MinRowNuminnumber,--分页小值
MaxRowNuminnumber,--分页大值
TotalCountoutnumber,--总记录数
Cur_ReturnCuroutT_CURSOR
)
is
v_SourceTb1varchar2(3000);--动态表名1
v_SourceTb2varchar2(3000);--动态表名2
v_SourceTb3varchar2(3000);--动态表名3
v_SourceTb4varchar2(3000);--动态表名4
v_TotalCountvarchar2(50);--总记录数
v_sqlvarchar2(3000);--动态sql
begin
v_SourceTb1:=’’’’(select’’’’||FieldStr||’’’’from’’’’||TbName||’’’’)SourceTb1’’’’;
v_SourceTb2:=’’’’(select*from’’’’||v_SourceTb1||’’’’where’’’’||RowFilter||’’’’’’’’||SortStr||’’’’)SourceTb2’’’’;
v_SourceTb3:=’’’’(selectrownumasRowindex,SourceTb2.*from’’’’||v_SourceTb2||’’’’whererownum<=’’’’||MaxRowNum||’’’’)SourceTb3’’’’;
v_SourceTb4:=’’’’(select*from’’’’||v_SourceTb1||’’’’where’’’’||RowFilter||’’’’)SourceTb4’’’’;
v_sql:=’’’’selectcount(*)asTotalCountfrom’’’’||v_SourceTb4;
executeimmediatev_sqlintov_TotalCount;
TotalCount:=v_TotalCount;
v_sql:=’’’’select*from’’’’||v_SourceTb3||’’’’whereRowIndex>=’’’’||MinRowNum;
openCur_ReturnCurforv_sql;
endPer_QuickPage;
ENDMaterialManage;
由于oracle有个rownum特性,所以分页的时候就是利用rownum来实现。如果大家还有什么更好的办法记得告诉我一声,多谢了,因为我测试了上面的分页方法效率并不是很高。
存储过程返回了两个参数:TotalCount :当前条件下的总记录数 Cur_ReturnCur :游标类型,就是所要读取的记录的集合
下面是ASP.NET中调用的代码:
以下是引用片段:
{
this.style.display=’none’;Codehighlighter1_2319_2685_Open_Text.style.display=’none’;Codehighlighter1_2319_2685_Closed_Image.style.display=’inline’;Codehighlighter1_2319_2685_Closed_Text.style.display=’inline’;
}"src="/wp-content/uploads/20170609/1017230.gif"align="top"/>{
this.style.display=’none’;Codehighlighter1_2319_2685_Closed_Text.style.display=’none’;Codehighlighter1_2319_2685_Open_Image.style.display=’inline’;Codehighlighter1_2319_2685_Open_Text.style.display=’inline’;
}"src="/wp-content/uploads/20170609/1017231.gif"align="top"/>/**////<summary>
///调用存储过程实现快速分页
///</summary>
///<paramname="TbName">表名称</param>
///<paramname="FieldStr">字段名称</param>
///<paramname="RowFilter">过滤条件</param>
///<paramname="SortStr">排序字段</param>
///<paramname="MinPageNum">分页小值</param>
///<paramname="MaxPageNum">分页大值</param>
///<paramname="TotalCount">总记录(需要返回)</param>
///<returns>DataTable</returns>
publicDataTableQuickPage(stringTbName,stringFieldStr,stringRowFilter,stringSortStr,intMinRowNum,intMaxRowNum,refintRecordCount)
{
this.style.display=’none’;Codehighlighter1_2824_4736_Open_Text.style.display=’none’;Codehighlighter1_2824_4736_Closed_Image.style.display=’inline’;Codehighlighter1_2824_4736_Closed_Text.style.display=’inline’;
}"src="/wp-content/uploads/20170609/1017230.gif"align="top"/>{
this.style.display=’none’;Codehighlighter1_2824_4736_Closed_Text.style.display=’none’;Codehighlighter1_2824_4736_Open_Image.style.display=’inline’;Codehighlighter1_2824_4736_Open_Text.style.display=’inline’;
}"src="/wp-content/uploads/20170609/1017231.gif"align="top"/>{
OracleConnectionconn=newOracleConnection(ConfigurationSettings.AppSettings["OracleConnstr"].ToString());
OracleCommandcmd=newOracleCommand();
cmd.Connection=conn;
cmd.CommandText="MaterialManage.Per_QuickPage";
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("TbName",OracleType.VarChar,50);//表名
cmd.Parameters["TbName"].Direction=ParameterDirection.Input;
cmd.Parameters["TbName"].Value=TbName;
cmd.Parameters.Add("FieldStr",OracleType.VarChar,3000);//字段集
cmd.Parameters["FieldStr"].Direction=ParameterDirection.Input;
cmd.Parameters["FieldStr"].Value=FieldStr;
cmd.Parameters.Add("RowFilter",OracleType.VarChar,3000);//过滤条件
cmd.Parameters["RowFilter"].Direction=ParameterDirection.Input;
cmd.Parameters["RowFilter"].Value=RowFilter;
cmd.Parameters.Add("SortStr",OracleType.VarChar,3000);//排序字段
cmd.Parameters["SortStr"].Direction=ParameterDirection.Input;
cmd.Parameters["SortStr"].Value=SortStr;
cmd.Parameters.Add("MinRowNum",OracleType.Number);//分页小值
cmd.Parameters["MinRowNum"].Direction=ParameterDirection.Input;
cmd.Parameters["MinRowNum"].Value=MinRowNum;
cmd.Parameters.Add("MaxRowNum",OracleType.Number);//分页大值
cmd.Parameters["MaxRowNum"].Direction=ParameterDirection.Input;
cmd.Parameters["MaxRowNum"].Value=MaxRowNum;
cmd.Parameters.Add("TotalCount",OracleType.Number);//页总记录数
cmd.Parameters["TotalCount"].Direction=ParameterDirection.Output;
cmd.Parameters["TotalCount"].Value=0;
cmd.Parameters.Add("Cur_ReturnCur",OracleType.Cursor);//返回的游标
cmd.Parameters["Cur_ReturnCur"].Direction=ParameterDirection.Output;
DataSetDs=newDataSet();
OracleDataAdapteradapter=newOracleDataAdapter(cmd);
adapter.Fill(Ds);
conn.Close();
//总记录数
RecordCount=int.Parse(cmd.Parameters["TotalCount"].Value.ToString());
returnDs.Tables[0];
}
(作者:佚名责任编辑:幽灵)
欢迎在新浪微博上关注我们
相关文章
评论