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永不言拜
新聞熱點
疑難解答