zoukankan      html  css  js  c++  java
  • [转载]asp.net中使用Row_Number函数分页

            网上的文章都是使用top之类的SQLServer2000中的方法分页,其实SQLServer2005之后的Row_Number()函数分页才是最好的方法,语句简单、思路清晰,可能是VS2008对Row_Number函数支持不好,不能自动生成,造成很多人不用它吧,下面分享一下我用Row_Number函数实现分页的过程。

    a) 首先在DataAdapter中新建名字为QueryThreadCountByForumId、返回值为Scalar的Query,SQL语句为:SELECT COUNT(*) AS Expr1 FROM rp_threads WHERE (ForumId = @ForumId)

    b) 在DataAdapter中新建名为字GetDataByForumId的Query,SQL语句为:SELECT * FROM (SELECT ThreadId, IsVisible, LastUpdateDate, DateLine, Subject, ForumId, ROW_NUMBER() OVER (ORDER BY DateLine) rownum FROM rp_threads WHERE ForumId = @ForumId) t WHERE rownum > @startRowIndex AND rownum <= @startRowIndex + @maximumRows。由于VS2008的设计器不支持窗口函数Over,因此会提示警告,不管它。对于使用Over的语句,VS2008不会帮助生成DataSet字段和参数,不过好在默认的GetData语句已经帮我们生成好了字段,虽然GetDataByForumId比GetData多一个rownum字段,不过新建的Query只要不是比GetData生成的字段少就行,多的部分会忽略。下面主要工作是手动建参数,选中GetDataByForumId方法,修改Parameters属性,在其中建三个Int32类型属性ForumId、startRowIndex、maximumRows,各自对应的ParameterName分别填@ForumId、@startRowIndex、@maximumRows。

    c) 在界面中使用ObjectDataSource数据源,TypeName填DataAdapter的类名,SelectCountMethod填QueryThreadCountByForumId、SelectMethod填GetDataByForumId,SelectParameters使用ForumId这一个属性,默认生成的maximumRows、startRowIndex 删掉就行,因为分页会自动填充他们两个。

    文章转载自:http://www.rupeng.com/forum/thread-11334-1-1-uid7.html 

  • 相关阅读:
    BZOJ 1191 HNOI2006 超级英雄hero
    BZOJ 2442 Usaco2011 Open 修建草坪
    BZOJ 1812 IOI 2005 riv
    OJ 1159 holiday
    BZOJ 1491 NOI 2007 社交网络
    NOIP2014 D1 T3
    BZOJ 2423 HAOI 2010 最长公共子序列
    LCA模板
    NOIP 2015 D1T2信息传递
    数据结构
  • 原文地址:https://www.cnblogs.com/babycool/p/2519673.html
Copyright © 2011-2022 走看看