一个通用的分页存储过程..............

本文分享了一种使用SQL存储过程实现高效分页查询的方法,并提供了详细的C#调用示例。该方法通过灵活配置参数来适应不同的分页需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前一阵子好多人都在问关于分页的问题,我自认为写了一个还不错的分页存储过程,

拿出来给大家分享,还有调用方法(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;

}

--------------------------------------------------------------------------------------

 这个方法和存储过程都没问题,都试过了,其实效率吗,呵呵,还是比较理想的,

希望大家能有所收获,欢迎指点和探讨。。。。。。。。。。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值