如果没有存储过程,如何在 ASP.NET 中对结果集进行分页?
如果没有存储过程,如何在 ASP.NET 中对从 SQL Server 检索的结果集进行分页?
Without stored procedures, how do you page result sets retrieved from SQL Server in ASP.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例如,您可以使用 LINQ:
然后显示这 25 个客户。
请参阅 Scott Guthrie 的出色 使用 LINQ-to-SQL - 第 6 节 - 使用服务器端分页检索产品。
另一种选择(在 SQL Server 2005 及更高版本上)是使用有序 CTE(通用表表达式) - 类似这样:
您基本上使用
ROW_NUMBER
函数在排序条件上定义 CTE,然后您可以随意选择其中的任意数量(此处:150 到 200 之间的数量)。这是非常高效且非常有用的服务器端分页。将这个 CTE 与您的实际数据表结合起来,您就可以检索您需要的任何内容!Marc
PS:好吧,所以OP手头只有SQL Server 2000,所以CTE将无法工作:-(
如果你无法更新到SQL Server 2005或.NET 3.5,恐怕你唯一可行的选择您可以执行类似的操作 - 请参阅此博客文章 使用 SQL Server 2000 实现高效、动态的服务器端分页,或使用 SQL Server 存储过程进行分页
You could use LINQ, for instance:
and then display those 25 customers.
See Scott Guthrie's excellent Using LINQ-to-SQL - section 6 - retrieve products with server side paging.
Another option (on SQL Server 2005 and up) is to use ordered CTE's (Common Table Expression) - something like this:
You basically define a CTE over your sort critiera using the
ROW_NUMBER
function, and then you can pick any number of those at will (here: those between 150 and 200). This is very efficient and very useful server-side paging. Join this CTE with your actual data tables and you can retrieve anything you need!Marc
PS: okay, so the OP only has SQL Server 2000 at hand, so the CTE won't work :-(
If you cannot update to either SQL Server 2005, or .NET 3.5, I'm afraid your only viable option really is stored procedures. You could do something like this - see this blog post Efficient and DYNAMIC Server-Side paging with SQL Server 2000, or Paging with SQL Server Stored Procedures
最好的方法是使用 ORM,它将为您生成动态分页代码 - LINQ To SQL、NHibernate、Entity Framework、SubSonic等。
如果结果集较小,则可以使用 DataPager、PagedDataSource,或手动使用 LINQ Skip 和 Take 命令。
The best is to use an ORM which will generate dynamic paging code for you - LINQ To SQL, NHibernate, Entity Framework, SubSonic, etc.
If you have a small result set, you can page on the server using either DataPager, PagedDataSource, or manually using LINQ Skip and Take commands.
(新答案,因为您正在使用 SQL Server 2000、.NET 2.0,并且不想使用 ORM)
在 SQL Server 2000 中有两种处理分页的方法:
如果您有一个与没有漏洞,您可以执行一个 SQL 字符串,其内容类似于
SELECT Name, Title FROM Customers WHERE CustomerID BETWEEN @low 和 @high
- @low 和 @high 是根据页面大小计算的参数以及您所在的页面。有关该内容的更多信息此处< /a>.如果您没有顺序 ID,则最终会使用最小 ID 和 @@rowcount 来选择范围。例如,SET @@rowcount 20;从客户中选择姓名、职务,其中 CustomerID > @low' - 根据页面大小和页面或根据最后显示的 CustomerID 计算 @low。 此处提供了有关该方法的一些信息。 p>
如果您有一个小数据集,您可以在 .NET 代码中对其进行分页,但效率较低。我推荐 PagedDataSource,但如果您想自己编写它,您可以将记录从 SqlDataReader 读取到数组中,然后 使用 Array.Range 函数进行分页。
(new answer since you're using SQL Server 2000, .NET 2.0, and don't want to use an ORM)
There are two ways to handle paging in SQL Server 2000:
If you have a ID column that's sequential with no holes, you can execute a SQL string that says something like
SELECT Name, Title FROM Customers WHERE CustomerID BETWEEN @low and @high
- @low and @high being parameters which are calculated based on the page size and page that you're on. More info on that here.If you don't have a sequential ID, you end up using a minimum ID and @@rowcount to select a range. For instance,
SET @@rowcount 20; SELECT Name, Title FROM Customers WHERE CustomerID > @low'
- either calculating @low from the page size and page or from the last displayed CustomerID. There's some info on that approach here.If you have a small dataset, you can page through it in .NET code, but it's less efficient. I'd recommend the PagedDataSource, but if you want to write it yourself you can just read your records from a SqlDataReader into an Array and then use the Array.Range function to page through it.
这就是我在 ASP.NET 2.0 应用程序中使用 AJAX 处理所有分页和排序的方式。
http: //programming.top54u.com/post/AJAX-GridView-Paging-and-Sorting-using-C-sharp-in-ASP-Net.aspx
This is how I handled all of my paging and sorting with AJAX in my ASP.NET 2.0 application.
http://programming.top54u.com/post/AJAX-GridView-Paging-and-Sorting-using-C-sharp-in-ASP-Net.aspx
我的一般方法通常是创建两个表来对结果进行分页。第一个是信息表,其中包含搜索 ID 标识列以及最小和最大行数。第二个表包含实际结果,并具有行号的标识列。我插入第二个表并获取最小和最大行并将它们存储在第一个表中。然后我可以通过仅选择我想要的行来进行翻页。我通常通过在插入之前使用代码在 24 小时后使结果过期。我通常使用存储过程来为我执行插入操作,但您也可以在没有存储过程的情况下完成此操作。
这样做的优点是只执行一次更复杂的 sql 搜索。并且数据集在页面显示之间不会发生变化。它是数据的快照。它还可以简化服务器端排序。我只需按顺序选择这些行并重新插入到第二个表中。
Well my general approach is usually to create two tables for the results to be paged. The first is an info table that has a search id identity column and has the min and max row numbers. The second table contains the actual results and has an identity column for the row number. I insert into the second table and get the min and max rows and store them in the first table. Then I can page through by selecting just the rows I want. I usually expire the results after 24 hours by using code right before the insert. I usually use a stored procedure to do the inserts for me but you could do it without a stored procedure.
This has the advantage of only performing the more complex sql search once. And the dataset won't change between page displays. It is a snapshot of the data. It also can ease server side sorting. I just have to select those rows in order and reinsert into the second table.