动态分页实现

 

ContractedBlock.gif ExpandedBlockStart.gif Code
//分页存储过程

CREATE    procedure Pager
@sqlstr nvarchar(
4000), --查询字符串
@currentpage 
int--第N页
@pagesize 
int --每页行数
as
set nocount on
declare @P1 
int--P1是游标的id
 @rowcount 
int,
 @pagecount 
int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt
=1,@ccopt=1,@rowcount=@rowcount output
 select ceiling(
1.0*@rowcount/@pagesize) as pagecount,@rowcount as recordcount--,@currentpage as 当前页 
set @pagecount = ceiling(1.0*@rowcount/@pagesize)
IF @currentpage
>=@pagecount
   
set @currentpage=@pagecount
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,
16,@currentpage,@pagesize 
exec sp_cursorclose @P1

set nocount off
GO

//实现代码:



//执行分页程序
        public DataTable ExecPager(string sql, int  curPage, int pageSize, out int pageCount , out int recordCount)
        {
            
//Set up a return value
            DataTable dt = new DataTable();
            DataSet ds 
= null;
           
//Create a parameter
            SqlParameter[] parms ={
                XNCJWC.Utility.Common.MakeInParm(
"@sqlstr",SqlDbType.VarChar,4000, sql),
                XNCJWC.Utility.Common.MakeInParm(
"@currentpage",SqlDbType.Int, 4, curPage),
                XNCJWC.Utility.Common.MakeInParm(
"@pagesize",SqlDbType.Int, 4, pageSize)
            };
            
try
            {
                
//Execute the query 
                ds = SqlHelper.ExecuteReaderDataSet(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Pager", parms);
                dt 
= ds.Tables[1];
                pageCount 
= Int32.Parse(((DataRow)dt.Rows[0])[0].ToString());
                recordCount 
= Int32.Parse(((DataRow)dt.Rows[0])[1].ToString());
            }
            
catch (Exception e)
            {
                XNCJWC.Utility.Log.Error(e.Message.ToString());

                
throw e;// new Exception("Query Failed");
            }
            
return ds.Tables[2];
        }

   
------------------------------------------------------------------------------------------------------------

pager类:

public class Pager
    {
       
private int _curPage ; //当前页数
       private int _recordCount ;//总记录数
       private int _pageSize  = 20; //每页记录数
       private int _pageCount;  //页数
       private string _strSql;  //sql语句
       private DataTable _dt;    //当前页记录集
          public Pager()
        {
         _curPage 
= 1 ;
         _recordCount 
= 0 ;        
         _pageCount 
= 0;
         _strSql 
= "";
        }
        
public Pager(string  sql, int curpage)
        {
            _recordCount 
= 0;
            _pageCount 
= 0;
            _strSql 
= sql;
            _curPage 
= curpage;
        }
       
/// <summary>
       
/// 分页
       
/// </summary>
        public void StartPage()
        {
            _dt 
= ExecPager(this._strSql, this._curPage, this._pageSize, out _pageCount, out _recordCount);              

        }
        
public int CurPage
        {
            
get { return this._curPage; }
            
set { this._curPage = value; }
        }
        
public int RecordCount
        {
            
get { return this._recordCount; }
            
set { this._recordCount = value; }
        }
        
public int PageSize
        {
            
get { return this._pageSize; }
            
set { this._pageSize = value; }
        }
        
public int PageCount
        {
            
get { return this._pageCount; }
            
set { this._pageCount = value; }
        }
        
public string StrSql
        {
            
get { return this._strSql; }
            
set { this._strSql = value; }
        }

        
public DataTable Dt
        {
            
get { return this._dt; }
            
set { this._dt = value; }
        }
       

    }



转载于:https://www.cnblogs.com/hubcarl/archive/2009/05/10/1453502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值