标签:
1.创建package
CREATE OR REPLACE PACKAGE pkg_query AS TYPE cur_query IS REF CURSOR; END pkg_query;
2.创建存储过程
create or replace procedure Sp_GetPageList (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(3000) := ‘‘; --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 Sp_GetPageList;
3.c#调用存储过程
DbHelperOra类中添加RunProcedure函数
/// <summary> /// 执行存储过程 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="TableName">表名</param> /// <param name="strWhere">查询条件</param> /// <param name="orderColumn">排序的列</param> /// <param name="orderStyle">排序方式</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">每页显示记录条数</param> /// <param name="rowCount">总记录数</param> /// <param name="pageCount">总页数</param> /// <returns></returns> public static DataSet RunProcedure(string storedProcName, string TableName, string strWhere, string orderColumn, string orderStyle, int pageIndex, int pageSize, out int rowCount, out int pageCount) { using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); OracleDataAdapter sqlDA = new OracleDataAdapter(storedProcName, connection); rowCount = 0; pageCount = 0; OracleParameter[] paras ={ new OracleParameter("p_tableName",TableName), new OracleParameter("p_strWhere",strWhere), new OracleParameter("p_orderColumn",orderColumn), new OracleParameter("p_orderStyle",orderStyle), new OracleParameter("p_curPage",pageIndex), new OracleParameter("p_pageSize",pageSize), new OracleParameter("p_totalRecords",rowCount), new OracleParameter("p_totalPages",pageCount), new OracleParameter("v_cur", OracleType.Cursor) //返回的游标 }; paras[6].Direction = ParameterDirection.Output; paras[7].Direction = ParameterDirection.Output; paras[8].Direction = ParameterDirection.Output; sqlDA.SelectCommand.Parameters.AddRange(paras); sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;//设置 使用存储过程 sqlDA.Fill(dataSet); connection.Close(); rowCount = Convert.ToInt32(paras[6].Value); pageCount = Convert.ToInt32(paras[7].Value); return dataSet; } }
调用RunProcedure函数
/// <summary> /// 通用数据分页 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="TableName">表名</param> /// <param name="strWhere">查询条件</param> /// <param name="orderColumn">排序的列</param> /// <param name="orderStyle">排序方式</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">每页显示记录条数</param> /// <param name="rowCount">总记录数</param> /// <param name="pageCount">总页数</param> public DataTable GetPageList(string strWhere, string TableName, string orderColumn, string orderStyle, int curPage, int pageSize, out int totalRecords, out int totalPages) { DataSet ds = DbHelperOra.RunProcedure("Sp_GetPageList", TableName, strWhere, orderColumn, orderStyle, curPage, pageSize, out totalRecords, out totalPages); DataTable dt = new DataTable(); if (ds != null && ds.Tables.Count > 0) { dt = ds.Tables[0]; } return dt; }
标签:
原文地址:http://www.cnblogs.com/bweb/p/4747482.html