SqlParameter[] parameters ={
new SqlParameter("@sql", SqlDbType.NChar,Sql.Length),
new SqlParameter("@key", SqlDbType.NChar,key.Length),
new SqlParameter("@PageIndex", SqlDbType.Int,8),
new SqlParameter("@PageSize", SqlDbType.Int,8),
new SqlParameter("@count", SqlDbType.Int,8)};
ALTER PROCEDURE [dbo].[Proc_Pager]
(
@sql nvarchar(4000),--查询sql语句包括条件
@key nvarchar(100), --排序字段
@PageIndex int, --当前页码
@PageSize int, --每页显示数
@count int output --总共行数
)
AS
declare @strSql nvarchar(4000) --查询的sql语句
declare @strCount nvarchar(4000) --查询总记录数的sql语句
declare @c int
BEGIN
--数据集
set @strSql='WITH list AS ('+@sql+') SELECT * FROM (SELECT ROW_NUMBER() OVER (order by '+ @key+') RowNumber,* FROM list) t where RowNumber between ' + convert(varchar(20), ((@PageIndex - 1) * @PageSize + 1)) + ' and ' + convert(varchar(20),(@PageIndex * @PageSize))
exec(@strSql)
--总条数
set @strCount='WITH list AS ('+@sql+')
select @c=count(*) from list '
exec sp_executesql @strCount,N'@c int output',@count output
END