我的oracle分页存储过程

create or replace package TX_PKG_PAGEPACKAGE  is  
  type TX_RESULTLIST is ref cursor; 
  procedure TX_PRC_PAGINATION(tx_resultlist out TX_RESULTLIST,insql in varchar2,currentpage in number,pagecount in number); 
end;
create or replace package body TX_PKG_PAGEPACKAGE is
  procedure TX_PRC_PAGINATION(tx_resultlist out TX_RESULTLIST,
                           insql         in varchar2,
                           currentpage   in number,
                           pagecount     in number) as
                           strSql varchar2(2000);
  begin
    strSql:='select * from (select rownum P_TX_ROWNUM, P_TX_TABLE.* from ('|| insql ||') P_TX_TABLE where rownum <='|| pagecount * currentpage ||') where P_TX_ROWNUM > '||(currentpage - 1) * pagecount;
    open TX_RESULTLIST for strSql;
  end TX_PRC_PAGINATION;
end TX_PKG_PAGEPACKAGE;

以上是sql。
先声明一个游标,做返回结果集。
然后把要查询的sql当做参数传入,动态执行,返回结果集。

c#:

主方法:
public DataSet SelectDsByProcedure(IDbConnection conn, string procedureName, IDbDataParameter[] parameters)
        {
            DataSet ds = new DataSet();
            OracleDataAdapter sqlDA = new OracleDataAdapter();
            OracleCommand command = new OracleCommand(procedureName, (OracleConnection)conn);
            command.CommandType = CommandType.StoredProcedure;
            foreach (OracleParameter parameter in parameters)
            {
                command.Parameters.Add(parameter);
            }
            sqlDA.SelectCommand = command;
            sqlDA.Fill(ds);
            Connection.Close();
            return ds;
        }

程序中调用:
OracleParameter[] paramDic = {
    new OracleParameter("tx_resultlist",OracleType.Cursor),
    new OracleParameter("insql",OracleType.VarChar),
    new OracleParameter("currentpage",OracleType.Int32),
    new OracleParameter("pagecount",OracleType.Int32)       };
            paramDic[0].Direction = ParameterDirection.Output;
            paramDic[1].Value = "select * from student order by stu_gender";
            paramDic[2].Value = 1;
            paramDic[3].Value = 2;
            DataSet ds = db.SelectDsByProcedure("TX_PKG_PAGEPACKAGE.TX_PRC_PAGINATION", paramDic);

存储过程中没做一些判断。比如传入存储过程的参数是否合法。是否有结果集之类的。请自行添加。

posted @ 2017-08-18 18:29  吃馒头的火鸡  阅读(296)  评论(0编辑  收藏  举报