Oracle:Oracle分页储存过程的实现

本示例采用控制台应用测试效果:

1、创建Oracle包的定义。

View Code
--创建Oracle包的定义。
create or replace package Vic_FY is
type t_cursor is ref cursor;
procedure GetDataByPage(
p_tableName in varchar2,
p_fields in varchar2,
p_filter in varchar2,
p_sort in varchar2,
p_curPage in number,
p_pageSize in number,
p_cursor out t_cursor,
p_totalRecords out number
);
end Vic_FY;

2、创建包体。在包体中实现具体的分页存储过程。

View Code
--创建包体。在包体中实现具体的分页存储过程。
create or replace package body Vic_FY is
procedure GetDataByPage(
 p_tableName in varchar2,--要查询的表名
     p_fields in varchar2,--要查询的字段
     p_filter in varchar2,--过滤条件
     p_sort in varchar2,--排序字段及方向
     p_curPage in number,
    p_pageSize in number,
    p_cursor out t_cursor,
    p_totalRecords out number        
)
is
v_sql varchar2(1000):='';
v_startRecord number(4);
v_endRecord number(4);
begin
v_sql:='select to_number(count(*)) from '||p_tableName;
if p_filter is not null then                                                   --如果过滤条件不为空
   v_sql:=v_sql||' where 1=1 and '||p_filter;
end if;
execute immediate v_sql into p_totalRecords;                                   --给总记录数赋值

v_startRecord:=(p_curPage-1)*p_pageSize;                                       --开始记录为 (当前页-1)*页宽
v_endRecord:=p_curPage*p_pageSize;                                             --结束记录为 当前页*页宽

v_sql:='select '||p_fields||' from (select '||p_fields||' , rownum r from '||'(select '||p_fields||' from '||p_tableName;
if p_filter is not null then
   v_sql:=v_sql||' where 1=1 and '||p_filter;                                  --加过滤条件
end if;
if p_sort is not null then 
  v_sql:=v_sql||' order by '||p_sort;                                          --加排序字段
end if;

v_sql:=v_sql||') A where rownum<='||to_char(v_endRecord)||') B where r>='||to_char(v_startRecord);

open p_cursor for v_sql;

end GetDataByPage;
end Vic_FY;

3、测试存储过程
引用:using System.Data.OracleClient;

View Code
 int p_curPage = 1;
            int p_pageSize = 10;
            OracleConnection conn = new OracleConnection(@"Data Source=服务器;User Id=用户名; Password=密码; Unicode=True");
            OracleCommand cmd = new OracleCommand("Vic_FY.GetDataByPage", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("p_tableName", OracleType.VarChar).Value = "xm_xm";
            cmd.Parameters.Add("p_fields", OracleType.VarChar).Value = "mc,bm";
            cmd.Parameters.Add("p_filter", OracleType.VarChar).Value = "";
            cmd.Parameters.Add("p_sort", OracleType.VarChar).Value = "mc";
            cmd.Parameters.Add("p_curPage", OracleType.Number).Value = p_curPage;
            cmd.Parameters.Add("p_pageSize", OracleType.Number).Value = p_pageSize;
            cmd.Parameters.Add("p_cursor", OracleType.Cursor).Direction = ParameterDirection.Output;
            cmd.Parameters.Add("p_totalRecords", OracleType.Number).Direction = ParameterDirection.Output;
            conn.Open();
            OracleDataReader dr = cmd.ExecuteReader();
            for (int i = 0; i < dr.FieldCount; i++) {
                Console.Write(dr.GetName(i).PadLeft(10));
            }
            Console.WriteLine();
            Console.WriteLine("-------------------------------------------");
            while (dr.Read()) {
                for (int i = 0; i < dr.FieldCount; i++) {
                    Console.Write(dr[i].ToString().PadLeft(10));
                }
                Console.WriteLine();
            }
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("当前第 "+p_curPage+" 页 | 每页 "+p_pageSize+" 条 | 共 "+cmd.Parameters["p_totalRecords"].Value.ToString()+" 条记录");
          
            conn.Close();
            Console.ReadKey();

 

posted @ 2013-04-10 16:52  张小三、  阅读(252)  评论(0编辑  收藏  举报