1.首先在oracle中建預存程序,預存程序名庫 PROCEDURE prc_query
CREATE OR REPLACE PROCEDURE prc_query
(p_tableName in varchar2, --表名
p_strWhere in varchar2, --查詢條件
p_orderColumn in varchar2, --排序的列
p_orderStyle in varchar2, --排序方式
p_curPage in out Number, --當前頁
p_pageSize in out Number, --每頁顯示記錄條數
p_totalRecords out Number, --總記錄數
p_totalPages out Number, --總頁數
v_cur out pkg_query.cur_query) --返回的結果集
IS
v_sql VARCHAR2(1000) := ''; --sql語句
v_startRecord Number(4); --開始顯示的記錄條數
v_endRecord Number(4); --結束顯示的記錄條數
BEGIN
--記錄中總記錄條數
v_sql := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || p_tableName || ' WHERE 1=1';
IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
v_sql := v_sql || p_strWhere;
END IF;
EXECUTE IMMEDIATE v_sql INTO p_totalRecords;
--驗證頁面記錄大小
IF p_pageSize < 0 THEN
p_pageSize := 0;
END IF;
--根據頁大小計算總頁數
IF MOD(p_totalRecords,p_pageSize) = 0 THEN
p_totalPages := p_totalRecords / p_pageSize;
ELSE
p_totalPages := p_totalRecords / p_pageSize + 1;
END IF;
--驗證頁號
IF p_curPage < 1 THEN
p_curPage := 1;
END IF;
IF p_curPage > p_totalPages THEN
p_curPage := p_totalPages;
END IF;
--實現分頁查詢
v_startRecord := (p_curPage - 1) * p_pageSize + 1;
v_endRecord := p_curPage * p_pageSize;
v_sql := 'SELECT * FROM (SELECT A.*, rownum r FROM ' ||
'(SELECT * FROM ' || p_tableName;
IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
v_sql := v_sql || ' WHERE 1=1' || p_strWhere;
END IF;
IF p_orderColumn IS NOT NULL or p_orderColumn <> '' THEN
v_sql := v_sql || ' ORDER BY ' || p_orderColumn || ' ' || p_orderStyle;
END IF;
v_sql := v_sql || ') A WHERE rownum <= ' || v_endRecord || ') B WHERE r >= '
|| v_startRecord;
DBMS_OUTPUT.put_line(v_sql);
OPEN v_cur FOR v_sql;
END prc_query;
2.在資料層中調用預存程序,返回一個datatable及二個參數,datatable儲存資料,curPage為當前頁碼(預存程序會根據傳入的數值查詢資料,返回datatable。),pageCount為總頁數, RecordCount為資料總條數
public class Paging
{
/// <summary>
///
/// </summary>
/// <param name="tName">表 名</param>
/// <param name="strSql">查詢條件</param>
/// <param name="sortRow">排序的列</param>
/// <param name="sortMethod">排序方式</param>
/// <param name="curPage">當前頁</param>
/// <param name="pageSize">每頁顯示記錄條數</param>
/// <param name="pageCount">總頁數</param>
/// <param name="RecordCount">總記錄數</param>
/// <returns></returns>
public DataTable QuickPage(string tName, string strSql, string sortRow, string sortMethod, int curPage, int pageSize, ref int pageCount, ref int RecordCount)
{
OracleConnection conn = new OracleConnection("server=.;Data Source=db;User ID=zsxl1;Password=zsxl;");
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "prc_query";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_tableName", OracleType.VarChar, 50); //表 名
cmd.Parameters["p_tableName"].Direction = ParameterDirection.Input;
cmd.Parameters["p_tableName"].Value = tName;
cmd.Parameters.Add("p_strWhere", OracleType.VarChar, 3000); //查詢條件
cmd.Parameters["p_strWhere"].Direction = ParameterDirection.Input;
cmd.Parameters["p_strWhere"].Value = strSql;
cmd.Parameters.Add("p_orderColumn", OracleType.VarChar, 3000); //排序的列
cmd.Parameters["p_orderColumn"].Direction = ParameterDirection.Input;
cmd.Parameters["p_orderColumn"].Value = sortRow;
cmd.Parameters.Add("p_orderStyle", OracleType.VarChar, 3000); //排序方式
cmd.Parameters["p_orderStyle"].Direction = ParameterDirection.Input;
cmd.Parameters["p_orderStyle"].Value = sortMethod;
cmd.Parameters.Add("p_curPage", OracleType.Number); //當前頁
cmd.Parameters["p_curPage"].Direction = ParameterDirection.Input;
cmd.Parameters["p_curPage"].Value = curPage;
cmd.Parameters.Add("p_pageSize", OracleType.Number); //每頁顯示記錄條數
cmd.Parameters["p_pageSize"].Direction = ParameterDirection.Input;
cmd.Parameters["p_pageSize"].Value = pageSize;
cmd.Parameters.Add("p_totalRecords", OracleType.Number); //總記錄數
cmd.Parameters["p_totalRecords"].Direction = ParameterDirection.Output;
cmd.Parameters["p_totalRecords"].Value = 0;
cmd.Parameters.Add("p_totalPages", OracleType.Number); //總頁數
cmd.Parameters["p_totalPages"].Direction = ParameterDirection.Output;
cmd.Parameters["p_totalPages"].Value = 0;
cmd.Parameters.Add("v_cur", OracleType.Cursor); //返回的遊標
cmd.Parameters["v_cur"].Direction = ParameterDirection.Output;
DataSet Ds = new DataSet();
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
adapter.Fill(Ds);
conn.Close();
//總記錄數
RecordCount = int.Parse(cmd.Parameters["p_totalRecords"].Value.ToString());
//總頁數
pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(cmd.Parameters["p_totalPages"].Value.ToString())));
//當前頁碼
//curPage = int.Parse(cmd.Parameters["p_curPage"].Value.ToString());
//TotalPages = int.Parse(cmd.Parameters["p_totalPages"].Value.ToString());
return Ds.Tables[0];
}
}
3。表現層調用方法QuickPage