展开全部
/*=======================================2008.3.19 笨笨熊 18071777=============================================================*/
Create procedure [dbo].[Pager]
(
@Psql nvarchar(4000), --生成dataset的语句
@PNum int, --显示第几页
@PSize int, --显示多少条
@Sort nvarchar(200) = null, --排序语句 如:order by id desc
@RowNumName nvarchar(50), --ROW_NUMBER别名
@Prcount int out, --返回记录总数
@Pcount int out --返回分页总数
)
as
set nocount on
declare @sqlTmp nvarchar(1000) --存放SQL语句
declare @sqlTmpCount nvarchar(1000) --存放查询记录总数量SQL语句
--计算范围
declare @Pmax int
declare @Pmin int
set @Pmax = @pnum*@psize --当前页*页大小 = 页最大值
set @Pmin = @Pmax - @psize +1 --页最大值 - 页大小 + 1 = 页最小值
set @sqlTmp='select * from ('+@Psql+')as temptb where '+@rowNumName+' BETWEEN '+cast(@Pmin as varchar(4))+' and '+cast(@Pmax as varchar(4))+''+@sort
set @sqlTmpCount='select @Prcount=count(*) from ('+@Psql+') as temptb'
----取得查询记录总数量-----
exec sp_executesql @sqlTmpCount,N'@Prcount int out ',@Prcount out
--取得分页总数
set @Pcount=(@Prcount+@pSize-1)/@pSize
exec sp_executesql @sqlTmp
set nocount off
/*调用测试
declare @Prcount int
declare @Pcount int
exec Pager 'select row_number() over(order by picid) AS rownum,* FROM userpic',1,3,'','rownum',1,1
print @Pcount*/
业务层调用:
/// <summary>
/// 根据用户自定义商品排序,分页取得用户收藏的商品
/// </summary>
/// <param name="pageIndex">当前页索引</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="recordCount">输出参数:总记录数</param>
/// <param name="pageCount">输出参数:总页数</param>
/// <returns>数据集(DataSet)</returns>
public DataSet GetShopsByPage(int pageIndex, int pageSize, out int recordCount, out int pageCount)
{
sql = "select row_number() over(order by picid) AS rownum,* FROM userpic";
return objDAO.GetDataByPage(sql, pageIndex, pageSize, "", "rownum", out recordCount, out pageCount);
}
数据访问层方法:
/// <summary>
/// 函数: 执行分页存储过程 , 返回数据集,适用于Sqlserver2005数据库
/// </summary>
/// <param name="psql">生成dataset的语句</param>
/// <param name="pNum">显示第几页</param>
/// <param name="pSize">显示多少条</param>
/// <param name="sort">排序语句 如:order by id desc </param>
/// <param name="rowNumName">ROW_NUMBER别名</param>
/// <param name="rCount">记录总数</param>
/// <param name="pCount">页总数</param>
/// <returns>返回数据集</returns>
public override DataSet GetDataByPage(string psql, int pNum, int pSize, string sort, string rowNumName, out int rCount, out int pCount)
{
OpenConn();
sqlCmd = new SqlCommand("Pager", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@Psql", SqlDbType.NVarChar).Value = psql;
sqlCmd.Parameters.Add("@pNum", SqlDbType.Int).Value = pNum;
sqlCmd.Parameters.Add("@Psize", SqlDbType.Int).Value = pSize;
sqlCmd.Parameters.Add("@sort", SqlDbType.NVarChar).Value = sort;
sqlCmd.Parameters.Add("@rowNumName", SqlDbType.NVarChar).Value = rowNumName;
sqlCmd.Parameters.Add("@Prcount", SqlDbType.Int).Direction = ParameterDirection.Output;
sqlCmd.Parameters.Add("@Pcount", SqlDbType.Int).Direction = ParameterDirection.Output;
//数据集对象
ds = new DataSet();
sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = sqlCmd;
//数据填充
sqlDa.Fill(ds);
//从输出参数中读取[总记录数]
rCount = (int)sqlCmd.Parameters["@Prcount"].Value;
//从输出参数中读取[总页数]
pCount = (int)sqlCmd.Parameters["@Pcount"].Value;
CloseConn();
return ds;
}
Create procedure [dbo].[Pager]
(
@Psql nvarchar(4000), --生成dataset的语句
@PNum int, --显示第几页
@PSize int, --显示多少条
@Sort nvarchar(200) = null, --排序语句 如:order by id desc
@RowNumName nvarchar(50), --ROW_NUMBER别名
@Prcount int out, --返回记录总数
@Pcount int out --返回分页总数
)
as
set nocount on
declare @sqlTmp nvarchar(1000) --存放SQL语句
declare @sqlTmpCount nvarchar(1000) --存放查询记录总数量SQL语句
--计算范围
declare @Pmax int
declare @Pmin int
set @Pmax = @pnum*@psize --当前页*页大小 = 页最大值
set @Pmin = @Pmax - @psize +1 --页最大值 - 页大小 + 1 = 页最小值
set @sqlTmp='select * from ('+@Psql+')as temptb where '+@rowNumName+' BETWEEN '+cast(@Pmin as varchar(4))+' and '+cast(@Pmax as varchar(4))+''+@sort
set @sqlTmpCount='select @Prcount=count(*) from ('+@Psql+') as temptb'
----取得查询记录总数量-----
exec sp_executesql @sqlTmpCount,N'@Prcount int out ',@Prcount out
--取得分页总数
set @Pcount=(@Prcount+@pSize-1)/@pSize
exec sp_executesql @sqlTmp
set nocount off
/*调用测试
declare @Prcount int
declare @Pcount int
exec Pager 'select row_number() over(order by picid) AS rownum,* FROM userpic',1,3,'','rownum',1,1
print @Pcount*/
业务层调用:
/// <summary>
/// 根据用户自定义商品排序,分页取得用户收藏的商品
/// </summary>
/// <param name="pageIndex">当前页索引</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="recordCount">输出参数:总记录数</param>
/// <param name="pageCount">输出参数:总页数</param>
/// <returns>数据集(DataSet)</returns>
public DataSet GetShopsByPage(int pageIndex, int pageSize, out int recordCount, out int pageCount)
{
sql = "select row_number() over(order by picid) AS rownum,* FROM userpic";
return objDAO.GetDataByPage(sql, pageIndex, pageSize, "", "rownum", out recordCount, out pageCount);
}
数据访问层方法:
/// <summary>
/// 函数: 执行分页存储过程 , 返回数据集,适用于Sqlserver2005数据库
/// </summary>
/// <param name="psql">生成dataset的语句</param>
/// <param name="pNum">显示第几页</param>
/// <param name="pSize">显示多少条</param>
/// <param name="sort">排序语句 如:order by id desc </param>
/// <param name="rowNumName">ROW_NUMBER别名</param>
/// <param name="rCount">记录总数</param>
/// <param name="pCount">页总数</param>
/// <returns>返回数据集</returns>
public override DataSet GetDataByPage(string psql, int pNum, int pSize, string sort, string rowNumName, out int rCount, out int pCount)
{
OpenConn();
sqlCmd = new SqlCommand("Pager", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@Psql", SqlDbType.NVarChar).Value = psql;
sqlCmd.Parameters.Add("@pNum", SqlDbType.Int).Value = pNum;
sqlCmd.Parameters.Add("@Psize", SqlDbType.Int).Value = pSize;
sqlCmd.Parameters.Add("@sort", SqlDbType.NVarChar).Value = sort;
sqlCmd.Parameters.Add("@rowNumName", SqlDbType.NVarChar).Value = rowNumName;
sqlCmd.Parameters.Add("@Prcount", SqlDbType.Int).Direction = ParameterDirection.Output;
sqlCmd.Parameters.Add("@Pcount", SqlDbType.Int).Direction = ParameterDirection.Output;
//数据集对象
ds = new DataSet();
sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = sqlCmd;
//数据填充
sqlDa.Fill(ds);
//从输出参数中读取[总记录数]
rCount = (int)sqlCmd.Parameters["@Prcount"].Value;
//从输出参数中读取[总页数]
pCount = (int)sqlCmd.Parameters["@Pcount"].Value;
CloseConn();
return ds;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询