分页存储过程
通用的分页存储过程
ALTER PROCEDURE p_Pagination
@tblName varchar(500), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
--@doCount int = 0, -- 返回记录总数, 非 0 值则返回
@OrderType int = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@CountAll bigint output --返回纪录总数用于计算页面数
AS
declare @strSQL nvarchar(4000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
declare @R bigint
if @strWhere != ''
begin
set @strSQL= N'select @R=count(*) from '+convert(nvarchar(200),@tblName)+' where '+@strWhere
exec SP_EXECUTESQL @strSQL, N' @R BIGINT OUTPUT', @R OUTPUT
set @CountAll= @R
end
else
begin
set @strSQL= N'select @R=count(*) from '+convert(nvarchar(200),@tblName)
exec SP_EXECUTESQL @strSQL, N' @R BIGINT OUTPUT', @R OUTPUT
set @CountAll= @R
end
/*
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ' + @tblName + ' where '+@strWhere
else
set @strSQL = 'select count(*) as Total from ' + @tblName + ''
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况:
else*/
begin
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '
from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '
from '+ @tblName + ' '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where [' + @fldName + ']' + @strTmp + '(['+ @fldName + '])
from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + ']
from ' + @tblName + '' + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from ' + @tblName + ' where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
****************************************************************************
C#调用方式
/// <summary>
/// 通用存储过程分页 by sk
/// </summary>
/// <param name="tblName">表名</param>
/// <param name="strGetFields">返回字段列表</param>
/// <param name="pageSize">每页显示的纪录数</param>
/// <param name="pageIndex">当前的页数</param>
/// <param name="fldName">排序的字段名</param>
/// <param name="orderType">排序的类型 0:Asc 1:Desc</param>
/// <param name="strWhere">SQL语句的Where条件</param>
/// <param name="CountAll">返回的纪录总数</param>
/// <returns>返回记录集DataSet</returns>
public static DataSet PageList(string tblName, string strGetFields, int pageSize, int pageIndex,
string fldName, string orderType, string strWhere, out int CountAll)
{
SqlParameter[] parameters ={ new SqlParameter("@tblName",SqlDbType.NVarChar,200),
new SqlParameter("@fldName",SqlDbType.NVarChar,500),
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@PageIndex",SqlDbType.Int),
new SqlParameter("@strGetFields",SqlDbType.NVarChar,200),
//new SqlParameter("@doCount",SqlDbType.Int),
new SqlParameter("@OrderType",SqlDbType.NVarChar,200),
new SqlParameter("@strWhere",SqlDbType.NVarChar,1000),
new SqlParameter("@CountAll",SqlDbType.Int)
};
parameters[0].Value = tblName;
parameters[1].Value = (fldName == null) ? "*" : fldName;
parameters[2].Value = (pageSize == 0) ? int.Parse(ConfigurationManager.AppSettings["PageSize"]) : pageSize;
parameters[3].Value = pageIndex;
parameters[4].Value = strGetFields;
//parameters[5].Value = doCount;
parameters[5].Value = orderType;
parameters[6].Value = strWhere;
parameters[7].Direction = ParameterDirection.Output;
DataSet ds = RunProcedure(LocalSqlServer, "p_Pagination", parameters, "ds");
CountAll = (int)parameters[7].Value;
return ds;
}