加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

GridView+分页存储过程

发布时间:2020-12-12 09:00:23 所属栏目:MsSql教程 来源:网络整理
导读:p style="FONT-SIZE: 10pt"存储过程中的参数可以只有2个:第几页和每页的行数总行数的可以使用简单的SELECT COUNT(1) FROM TableName来完成,总页就是总行数/每页的行数。把这几个算出来的数填充到GridView相应的属性中。当点第几页的时候,再调用分页存储过

<p style="FONT-SIZE: 10pt">存储过程中的参数可以只有2个:第几页和每页的行数总行数的可以使用简单的SELECT COUNT(1) FROM TableName来完成,总页就是总行数/每页的行数。把这几个算出来的数填充到GridView相应的属性中。当点第几页的时候,再调用分页存储过程来刷新数据,就可以了。 群主(87273411) 09:34:52create PROCEDURE Sp_Conn_Sort(@tblName?? varchar(255),?????? -- 表名


<p style="FONT-SIZE: 10pt">@strGetFields varchar(1000) = '',? -- 需要返回的列


<p style="FONT-SIZE: 10pt">@fldName varchar(255)='',????? -- 排序的字段名


<p style="FONT-SIZE: 10pt">@PageSize?? int = 40,????????? -- 页尺寸


<p style="FONT-SIZE: 10pt">@PageIndex? int = 1,?????????? -- 页码


<p style="FONT-SIZE: 10pt">@doCount? bit = 0,?? -- 返回记录总数,非 0 值则返回


<p style="FONT-SIZE: 10pt">@OrderType bit = 0,? -- 设置排序类型,非 0 值则降序@strWhere? varchar(1500)=''? -- 查询条件 (注意: 不要加 where))AS


<p style="FONT-SIZE: 10pt">declare @strSQL?? varchar(5000)?????? -- 主语句


<p style="FONT-SIZE: 10pt">declare @strTmp?? varchar(110)??????? -- 临时变量


<p style="FONT-SIZE: 10pt">declare @strOrder varchar(400)??????? -- 排序类型


<p style="FONT-SIZE: 10pt">?


<p style="FONT-SIZE: 10pt">if @doCount != 0


<p style="FONT-SIZE: 10pt">? begin


<p style="FONT-SIZE: 10pt">??? if @strWhere !=''


<p style="FONT-SIZE: 10pt">??? set @strSQL = 'select count(
) as Total from ' + @tblName + ' where <a href="mailto:'+@strWhere">'+@strWhere


<p style="FONT-SIZE: 10pt">??? else


<p style="FONT-SIZE: 10pt">??? set @strSQL = 'select count() as Total from ' + @tblName


<p style="FONT-SIZE: 10pt">? end?


<p style="FONT-SIZE: 10pt">--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况


<p style="FONT-SIZE: 10pt">else


<p style="FONT-SIZE: 10pt">begin


<p style="FONT-SIZE: 10pt">?


<p style="FONT-SIZE: 10pt">if @OrderType != 0


<p style="FONT-SIZE: 10pt">begin


<p style="FONT-SIZE: 10pt">??? set @strTmp = '<(select min'


<p style="FONT-SIZE: 10pt">set @strOrder = ' order by ' + @fldName +' desc'


<p style="FONT-SIZE: 10pt">--如果@OrderType不是0,就执行降序,这句很重要!


<p style="FONT-SIZE: 10pt">end


<p style="FONT-SIZE: 10pt">else


<p style="FONT-SIZE: 10pt">begin


<p style="FONT-SIZE: 10pt">??? set @strTmp = '>(select max'


<p style="FONT-SIZE: 10pt">??? set @strOrder = ' order by ' + @fldName +' asc'


<p style="FONT-SIZE: 10pt">end


<p style="FONT-SIZE: 10pt">?


<p style="FONT-SIZE: 10pt">if @PageIndex = 1


<p style="FONT-SIZE: 10pt">begin


<p style="FONT-SIZE: 10pt">??? if @strWhere != ''??


<p style="FONT-SIZE: 10pt">??? set @strSQL = 'select top ' + str(@PageSize) +' <a href="mailto:'+@strGetFields">'+@strGetFields+ '? from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder


<p style="FONT-SIZE: 10pt">???? else


<p style="FONT-SIZE: 10pt">???? set @strSQL = 'select top ' + str(@PageSize) +' <a href="mailto:'+@strGetFields">'+@strGetFields+ '? from '+ @tblName + ' '+ @strOrder


<p style="FONT-SIZE: 10pt">--如果是第一页就执行以上代码,这样会加快执行速度


<p style="FONT-SIZE: 10pt">end


<p style="FONT-SIZE: 10pt">else


<p style="FONT-SIZE: 10pt">begin


<p style="FONT-SIZE: 10pt">--以下代码赋予了@strSQL以真正执行的SQL代码


<p style="FONT-SIZE: 10pt">set @strSQL = 'select top ' + str(@PageSize) +' <a href="mailto:'+@strGetFields">'+@strGetFields+ '? from '


<p style="FONT-SIZE: 10pt">??? + @tblName + ' where ' + @fldName + '' + @strTmp + '('+ @fldName + ') from (select top ' + str((@PageIndex-1)
@PageSize) + ' '+ @fldName + ' from ' + @tblName + '' + @strOrder + ') as tblTmp)'+ @strOrder


<p style="FONT-SIZE: 10pt">?


<p style="FONT-SIZE: 10pt">if @strWhere != ''


<p style="FONT-SIZE: 10pt">??? set @strSQL = 'select top ' + str(@PageSize) +' <a href="mailto:'+@strGetFields">'+@strGetFields+ '? from '


<p style="FONT-SIZE: 10pt">??????? + @tblName + ' where ' + @fldName + '' + @strTmp + '('


<p style="FONT-SIZE: 10pt">??????? + @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '


<p style="FONT-SIZE: 10pt">??????? + @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '


<p style="FONT-SIZE: 10pt">??????? + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder


<p style="FONT-SIZE: 10pt">end


<p style="FONT-SIZE: 10pt">end??


<p style="FONT-SIZE: 10pt">exec (@strSQL) 群主(87273411) 09:35:07

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读