sqlserver存储过程和c#分页类简化你的代码!
在最近的项目中,由于要用到自定义分页的功能,本人就在网上找了个存储过程。结合c#写了个分页类。由于本人第一次写文章。写得不好,大家不要扔鸡蛋。。
下面是存储过程(sqlserver2000下通过)
--最通用的分页存储过程
-- 获取指定页的数据
create procedure pagination
@tblname varchar(255), -- 表名
@strgetfields varchar(1000) = '*', -- 需要返回的列
@fldname varchar(255)='', -- 排序的字段名
@pagesize int = 10, -- 页尺寸
@pageindex int = 1, -- 页码
@docount bit = 0, -- 返回记录总数, 非 0 值则返回
@ordertype bit = 0, -- 设置排序类型, 非 0 值则降序
@strwhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
as
declare @strsql varchar(5000) -- 主语句
declare @strtmp varchar(110) -- 临时变量
declare @strorder varchar(400) -- 排序类型
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) +' '[email protected]+ ' from ['+ @tblname +'] where ' + @strwhere + ' ' + @strorder
else
set @strsql = 'select top ' + str(@pagesize) +' '[email protected]+ ' from ['+ @tblname +'] '+ @strorder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin
--以下代码赋予了@strsql以真正执行的sql代码
set @strsql = 'select top ' + str(@pagesize) +' '[email protected]+ ' 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) +' '[email protected]+ ' 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)
go
下面是c#的代码
using system.data ;
using system.data.sqlclient ;
using microsoft.applicationblocks.data ;
using system.web ;
using system.web.ui ;
namespace rsslayer.pagehelper
{
/**//// <summary>
/// 分页类pagerhelper 的摘要说明。
/// </summary>
public class pagerhelper
{
private string connectionstring;
public pagerhelper(string tblname,string sortname,bool docount,string connectionstring)
{
this.tblname = tblname;
this.fldname = sortname ;
this.connectionstring = connectionstring ;
this.docount = docount;
}
public pagerhelper(string tblname,bool docount,
string strgetfields, string fldname,int pagesize,
int pageindex,bool ordertype,string strwhere,string connectionstring
)
{
this.tblname = tblname ;
this.docount = docount ;
this.strgetfields = strgetfields ;
this.fldname = fldname;
this.pagesize = pagesize ;
this.pageindex = pageindex;
this.ordertype = ordertype ;
this.strwhere = strwhere ;
this.connectionstring = connectionstring ;
}
/**//// <summary>
/// 得到记录集的构造函数
/// </summary>
/// <param name="tblname"></param>
/// <param name="strwhere"></param>
/// <param name="connectionstring"></param>
public pagerhelper(string tblname,string strwhere,string connectionstring)
{
this.tblname = tblname;
this.strwhere = strwhere ;
this.docount = true;
this.connectionstring = connectionstring ;
}
private string tblname;
public string tblname
{
get{return tblname;}
set{tblname =value;}
}
private string strgetfields="*";
public string strgetfields
{
get{return strgetfields ;}
set{strgetfields =value;}
}
private string fldname=string.empty;
public string fldname
{
get{return fldname ;}
set{fldname =value;}
}
private int pagesize =10;
public int pagesize
{
get{return pagesize ;}
set{pagesize =value;}
}
private int pageindex =1;
public int pageindex
{
get{return pageindex ;}
set{pageindex =value;}
}
private bool docount=false;
public bool docount
{
get{return docount ;}
set{docount =value;}
}
private bool ordertype=false;
public bool ordertype
{
get{return ordertype ;}
set{ordertype =value;}
}
private string strwhere=string.empty ;
public string strwhere
{
get{return strwhere ;}
set{strwhere =value;}
}
public idatareader getdatareader()
{
if(this.docount)
{
throw new argumentexception("要返回记录集,docount属性一定为false");
}
// system.web.httpcontext.current.response.write(pageindex);
return sqlhelper.executereader(connectionstring,commandtype.storedprocedure,"pagination",
new sqlparameter("@tblname",this.tblname),
new sqlparameter("@strgetfields",this.strgetfields),
new sqlparameter("@fldname",this.fldname),
new sqlparameter("@pagesize",this.pagesize),
new sqlparameter("@pageindex",this.pageindex),
new sqlparameter("@docount",this.docount),
new sqlparameter("@ordertype",this.ordertype),
new sqlparameter("@strwhere",this.strwhere)
);
}
public dataset getdataset()
{
if(this.docount)
{
throw new argumentexception("要返回记录集,docount属性一定为false");
}
return sqlhelper.executedataset(connectionstring,commandtype.storedprocedure,"pagination",
new sqlparameter("@tblname",this.tblname),
new sqlparameter("@strgetfields",this.strgetfields),
new sqlparameter("@fldname",this.fldname),
new sqlparameter("@pagesize",this.pagesize),
new sqlparameter("@pageindex",this.pageindex),
new sqlparameter("@docount",this.docount),
new sqlparameter("@ordertype",this.ordertype),
new sqlparameter("@strwhere",this.strwhere)
);
}
public int getcount()
{
if(!this.docount)
{
throw new argumentexception("要返回总数统计,docount属性一定为true");
}
return (int)sqlhelper.executescalar(connectionstring,commandtype.storedprocedure,"pagination",
new sqlparameter("@tblname",this.tblname),
new sqlparameter("@strgetfields",this.strgetfields),
new sqlparameter("@fldname",this.fldname),
new sqlparameter("@pagesize",this.pagesize),
new sqlparameter("@pageindex",this.pageindex),
new sqlparameter("@docount",this.docount),
new sqlparameter("@ordertype",this.ordertype),
new sqlparameter("@strwhere",this.strwhere)
);
}
}
}
如何调用???
假如我已经建立了2个类。一个是favlist数据库实体类,一个favlistcollection集合类。favlistcollection存储了favlist实体类的集合。
我可以这样写一个方法。
/**//// <summary>
/// 返回favlist集合,使用存储过程自定义分页。
/// </summary>
/// <param name="userid">数据库favlist的字段,用户id</param>
/// <param name="strwhere">查找的条件</param>
/// <param name="ordertype">排序,true表示desc,false表示asc</param>
/// <param name="fldname">排序的字段,只能是一个字段</param>
/// <param name="pagesize">每页的记录数</param>
/// <param name="pageindex">到第几页的参数,由1开始。1表示第一页,以此类推。</param>
/// <param name="recordcount">总记录数。</param>
/// <returns></returns>
public override favlistcollection getfavlistsbyuser(int userid, string strwhere,
bool ordertype, string fldname, int pagesize,
int pageindex,out int recordcount
)
{
recordcount = 0;
pagerhelper helper = new pagerhelper("vfavlist",strwhere,connectionstring); //vfavlist是view
recordcount = helper.getcount();
pagerhelper helper2 = new pagerhelper("vfavlist",false," * ",fldname,
pagesize,pageindex,ordertype,strwhere,connectionstring);
idatareader dr = helper2.getdatareader();
favlistcollection list = new favlistcollection();
while(dr.read())
{
list.add(populatefavlist(dr));
}
dr.close();
return list;
}
datagrid调用就不用说了吧。。
关于该分页的bug和局限性
bug:当排序那个字段内容相同的时候(例如:按时间来排序,而时间是一样的话。后面的记录会显示不出来。本人测试过)
局限性:排序只能一个字段,不能超过一个
出处:blog永不言拜
新闻热点
疑难解答