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;
}
///
/// 分页
///
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; }
}
}
//分页存储过程
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;
}
///
/// 分页
///
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; }
}
}