`
ihuashao
  • 浏览: 4531473 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

asp.net利用存储过程分页代码

阅读更多

下面是存储过程(sqlserver2000下通过)

--最通用的分页存储过程
--
获取指定页的数据

CREATEPROCEDUREPagination

@tblNamevarchar(255),--表名

@strGetFieldsvarchar(1000)='*',--需要返回的列

@fldNamevarchar(255)='',--排序的字段名

@PageSizeint=10,--页尺寸

@PageIndexint=1,--页码

@doCountbit=0,--返回记录总数,非0值则返回

@OrderTypebit=0,--设置排序类型,非0值则降序

@strWherevarchar(1500)=''--查询条件(注意:不要加where)

AS

declare@strSQLvarchar(5000)--主语句

declare@strTmpvarchar(110)--临时变量

declare@strOrdervarchar(400)--排序类型



if@doCount!=0

begin

if@strWhere!=''

set@strSQL='selectcount(*)asTotalfrom['+@tblName+']where'+@strWhere

else

set@strSQL='selectcount(*)asTotalfrom['+@tblName+']'

end

--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都
--
是@doCount为0的情况

else

begin



if@OrderType!=0

begin

set@strTmp='<(selectmin'

set@strOrder='orderby['+@fldName+']desc'

--如果@OrderType不是0,就执行降序,这句很重要!

end

else

begin

set@strTmp='>(selectmax'

set@strOrder='orderby['+@fldName+']asc'

end



if@PageIndex=1

begin

if@strWhere!=''

set@strSQL='selecttop'+str(@PageSize)+''+@strGetFields+'from['+@tblName+']where'+@strWhere+''+@strOrder

else

set@strSQL='selecttop'+str(@PageSize)+''+@strGetFields+'from['+@tblName+']'+@strOrder

--如果是第一页就执行以上代码,这样会加快执行速度

end

else

begin

--以下代码赋予了@strSQL以真正执行的SQL代码

set@strSQL='selecttop'+str(@PageSize)+''+@strGetFields+'from['+@tblName+']where['+@fldName+']'+@strTmp+'(['+@fldName+'])
from(selecttop
'+str((@PageIndex-1)*@PageSize)+'['+@fldName+']from['+@tblName+']'+@strOrder+')astblTmp)'+@strOrder



if@strWhere!=''

set@strSQL='selecttop'+str(@PageSize)+''+@strGetFields+'from['+@tblName+']where['+@fldName+']'+@strTmp+'(['+@fldName+'])from(selecttop'+str((@PageIndex-1)*@PageSize)+'['+@fldName+']
from[
'+@tblName+']where'+@strWhere+''+@strOrder+')astblTmp)and'+@strWhere+''+@strOrder

end

end

exec(@strSQL)
GO

下面是C#的代码

usingSystem.Data;
usingSystem.Data.SqlClient;
usingMicrosoft.ApplicationBlocks.Data;
usingSystem.Web;
usingSystem.Web.UI;
namespaceRssLayer.PageHelper
{
/**////<summary>
///分页类PagerHelper的摘要说明。
///</summary>

publicclassPagerHelper
{
privatestringconnectionString;



publicPagerHelper(stringtblname,stringsortname,booldocount,stringconnectionString)
{
this.tblName=tblname;
this.fldName=sortname;
this.connectionString=connectionString;
this.docount=docount;
}


publicPagerHelper(stringtblname,booldocount,
stringstrGetFields,stringfldName,intpagesize,
intpageindex,boolordertype,stringstrwhere,stringconnectionString
)
{
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>
///<paramname="tblname"></param>
///<paramname="strwhere"></param>
///<paramname="connectionString"></param>

publicPagerHelper(stringtblname,stringstrwhere,stringconnectionString)
{
this.tblName=tblname;
this.strwhere=strwhere;
this.docount=true;
this.connectionString=connectionString;
}


privatestringtblName;
publicstringTblName
{
get{returntblName;}
set{tblName=value;}
}


privatestringstrGetFields="*";
publicstringStrGetFields
</span
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics