在
web開發中,分頁是我們必須要解決的一個問題,今天我們一起來看看
ASP.Net調用ORACEL存儲過程實現分頁的方法。
包定義:
以下是引用片段: create or replace package MaterialManage is
TYPE T_CURSOR IS REF CURSOR;
Procedure Per_QuickPage
(
TbName in varchar2, --表名
FIEldStr in varchar2, --字段集
RowFilter in varchar2, --過濾條件
SortStr in varchar2, --排序集
RownumFIEldStr in varchar2, --分頁條件
TotalCount out number, --總記錄數
Cur_ReturnCur out T_CURSOR --返回的游標
);
end MaterialManage;
包主體:
以下是引用片段: create or replace package body MaterialManage is
Procedure Per_QuickPage
(
TbName in varchar2, --表 名
FIEldStr in varchar2, --字段集
RowFilter in varchar2, --過濾條件
SortStr in varchar2, --排序集
MinRowNum in number, --分頁小值
MaxRowNum in number, --分頁大值
TotalCount out number, --總記錄數
Cur_ReturnCur out T_CURSOR
)
is
v_SourceTb1 varchar2(3000); --動態表名1
v_SourceTb2 varchar2(3000); --動態表名2
v_SourceTb3 varchar2(3000); --動態表名3
v_SourceTb4 varchar2(3000); --動態表名4
v_TotalCount varchar2(50); --總記錄數
v_sql varchar2(3000); --動態sql
begin
v_SourceTb1 := ’’’’(select ’’’’|| FIEldStr ||’’’’ from ’’’’|| TbName ||’’’’) SourceTb1’’’’;
v_SourceTb2 := ’’’’(select * from ’’’’|| v_SourceTb1 ||’’’’ where ’’’’|| RowFilter ||’’’’ ’’’’|| SortStr ||’’’’) SourceTb2’’’’;
v_SourceTb3 := ’’’’(select rownum as Rowindex,SourceTb2.* from ’’’’|| v_SourceTb2 ||’’’’ where rownum<=’’’’|| MaxRowNum ||’’’’) SourceTb3’’’’;
v_SourceTb4 := ’’’’(select * from ’’’’|| v_SourceTb1 ||’’’’ where ’’’’|| RowFilter ||’’’’) SourceTb4’’’’;
v_sql := ’’’’select count(*) as TotalCount from ’’’’|| v_SourceTb4;
execute immediate v_sql into v_TotalCount;
TotalCount := v_TotalCount;
v_sql := ’’’’select * from ’’’’|| v_SourceTb3 ||’’’’ where RowIndex >=’’’’||MinRowNum;
open Cur_ReturnCur for v_sql;
end Per_QuickPage;
END MaterialManage;
由於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="/School/UploadFiles_7810/201106/20110603125417477.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="/School/UploadFiles_7810/201106/20110603125418925.gif" align="top" />/**//// <summary>
/// 調用存儲過程實現快速分頁
/// </summary>
/// <param name="TbName">表名稱</param>
/// <param name="FIEldStr">字段名稱</param>
/// <param name="RowFilter">過濾條件</param>
/// <param name="SortStr">排序字段</param>
/// <param name="MinPageNum">分頁小值</param>
/// <param name="MaxPageNum">分頁大值</param>
/// <param name="TotalCount">總記錄(需要返回)</param>
/// <returns>DataTable</returns>
public DataTable QuickPage(string TbName,string FIEldStr,string RowFilter,string SortStr,int MinRowNum,int MaxRowNum,ref int RecordCount)
{
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="/School/UploadFiles_7810/201106/20110603125417477.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="/School/UploadFiles_7810/201106/20110603125418925.gif" align="top" />{
OracleConnection conn = new OracleConnection(ConfigurationSettings.APPSettings["OracleConnstr"].ToString());
OracleCommand cmd = new OracleCommand();
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;
DataSet Ds = new DataSet();
OracleDataAdapter adapter= new OracleDataAdapter(cmd);
adapter.Fill(Ds);
conn.Close();
//總記錄數
RecordCount = int.Parse(cmd.Parameters["TotalCount"].Value.ToString());
return Ds.Tables[0];
}