前一阵子好多人都在问关于分页的问题,我自认为写了一个还不错的分页存储过程,
拿出来给大家分享,还有调用方法(C#) 过程和代码如下: -
-------------------------------------------------------------------------------------
ALTER PROCEDURE pro_Pagelist @tablename nvarchar(50),---表名
@fieldname nvarchar(350)='*',--字段名默认为全部
@pagesize int,--每页显示记录条数
@currentpage int,--但前的页数
@orderid nvarchar(50),--这个一般用主键(唯一标识的,如果不是唯一标示得到的数据可能不准)
@strWhere nvarchar(250),--where条件,注:调用时不加where 如:name='张三'
@sort int=0,--排序方式,1表示升序,0表示降序排列
@sortBy nvarchar(50),--要排序的字段
@rowcount int output,--总记录数,共有几条信息
@pagecount int output--总页数,共有多少页
AS declare @countsql nvarchar(350) ---保存总记录数和总条数说的SQL语句
declare @sql nvarchar(800) ---最终的SQL语句
declare @subsql nvarchar(500)--not in子sql语句
declare @tmpOrderid nvarchar(50) ---排序子句 --返回总记录数,并赋值给输出参数@rowcount
if @strWhere is not null and @strWhere!=''
set @countsql='select @totalcount=count(*) from '+@tablename+' where '+ @strWhere
else
set @countsql='select @totalcount=count(*) from '+@tablename
print @countsql
exec sp_executesql @countsql,N'@totalcount int out',@rowcount output
--判断字段名是否为空
if @fieldname is null or @fieldname=''
set @fieldname=' * ' --判断是否排序及排序方式
if @orderid is null or @orderid=''
set @tmpOrderid=' '
else
begin
if @sort=0
begin
if @sortBy is not null and @sortBy!=''
set @tmpOrderid=' order by '+@sortBy+' desc'
else
set @tmpOrderid=' order by '+@orderid+' desc'
end
else
begin
if @sortBy is not null and @sortBy!=''
set @tmpOrderid=' order by '+@sortBy+' asc'
else
set @tmpOrderid=' order by '+@orderid+' asc'
end
end
--计算页数
if @rowcount%@pagesize>0
set @pagecount =(@rowcount/@pagesize)+1;
else
set @pagecount=@rowcount/@pagesize;
--分页算法实现
if( @strWhere is not null and @strWhere!='')
begin
set @subsql='select top'+str(@pagesize*(@currentpage-1))+' '+@orderid+
' from '+@tablename+' where '+@strWhere
set @sql='select top'+str(@pagesize)+' '+@fieldname+
' from '+@tablename+' where '+@strWhere +' and '
+@orderid+' not in ('+@subsql+')'+@tmpOrderid
end
else
begin
set @subsql='select top'+str(@pagesize*(@currentpage-1))+' '+@orderid+
' from '+@tablename-- + @tmpOrderid
set @sql='select top'+str(@pagesize)+' '+@fieldname+' from '
+@tablename+' where '+@orderid+' not in ('+@subsql+')'+
@tmpOrderid
end
print @subsql
print @sql
exec(@sql)
对该存储过程的调用封装了一个方法(该方法返回一个DataSet)如下:
-----------------------------------------------------------------------------------
public DataSet GetCutPage(string tableName, string fieldName, int pageSize, int currentPage,string strWhere, string orderid, int sort,string sortBy, out int rowCount, out int pageCount)
{
SqlParameter[] prams ={
new SqlParameter("@tablename",SqlDbType.NVarChar),
new SqlParameter("@fieldname",SqlDbType.NVarChar),
new SqlParameter("@pagesize",SqlDbType.Int),
new SqlParameter("@currentpage",SqlDbType.Int),
new SqlParameter("@orderid",SqlDbType.NVarChar),
new SqlParameter("@strWhere",SqlDbType.NVarChar),
new SqlParameter("@sort",SqlDbType.Int),
new SqlParameter("@sortBy",SqlDbType.NVarChar),
new SqlParameter("@rowcount",SqlDbType.Int),
ew SqlParameter("@pagecount",SqlDbType.Int)
};
prams[0].Value = tableName;//表名
prams[1].Value =fieldName;//字段名
prams[2].Value = pageSize;//每页显示条数
prams[3].Value = currentPage;//当前页数
prams[4].Value = orderid;//主键
prams[5].Value = strWhere;//条件
prams[6].Value = sort;//排序方式,0表示降序,1表示升序
prams[7].Value = sortBy;//排序方式
prams[8].Direction = ParameterDirection.Output;//总记录数
prams[9].Direction = ParameterDirection.Output;//总页数 String connectionString=System.Configuration.ConfigurationManager.ConnectionStrings["*****"].ConnectionString;
SqlConnection conn=new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connnection=conn;
conn.Open();
cmd.CommandText = "pro_Pagelist";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(prams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
rowCount = (int)cmd.Parameters["@rowcount"].Value;
pageCount = (int)cmd.Parameters["@pagecount"].Value;
conn.Close();
return ds;
}
--------------------------------------------------------------------------------------
这个方法和存储过程都没问题,都试过了,其实效率吗,呵呵,还是比较理想的,
希望大家能有所收获,欢迎指点和探讨。。。。。。。。。。