简体   繁体   English

使用存储过程(SQL 2000)在C#中分页实现

[英]Paging implementation in C# using Stored Procedure (SQL 2000)

Anyone with a link to a sample code with paging that can be changed to work within a (10000+ records) table? 是否有人链接到带有分页的示例代码,可以将其更改为在(超过10000条记录)表中工作? I want to display the records on my asp.net page. 我想在我的asp.net页面上显示记录。

Cheers 干杯

Best practice for pagination, is to do it in your stored proc, so you can limit the amount of data you are pulling back, eg: 分页的最佳实践是在存储的proc中进行,因此可以限制要拉回的数据量,例如:

CREATE PROCEDURE dbo.GetSomeData
  @page INT,
  @size INT = 25
AS
BEGIN

    DECLARE @offset INT
    SET @offset = (@page - 1) * @size;

    WITH OrderedSet AS
    (
      SELECT Field1, Field2, Field3, ROW_NUMBER() OVER (ORDER BY Field1) AS 'Index'
      FROM SomeTable
    )
    SELECT Field1, Field2, Field3 FROM OrderedSet WHERE [Index] BETWEEN @offset AND (@offset + @size)
END
GO

That's specific to Sql Server, MySql is a little easier: 这是特定于Sql Server的,MySql要容易一些:

DELIMITER $$
CREATE PROCEDURE GetSomeData(IN page INT, IN size INT DEFAULT 25)
BEGIN
  DECLARE offset INT DEFAULT 0
  SET offset = (page - 1) * size;

  SELECT `Field1`, `Field2`, `Field3` FROM `SomeTable` LIMIT offset, (offset + size);
END$$
DELIMITER ;

EDIT , seems like you are using Sql 2000. 编辑 ,似乎您正在使用Sql 2000。

Pagination is a little trickier on Sql 2000, as you don't have any built in functions for generating an efficient page count. 分页在Sql 2000上有点棘手,因为您没有任何用于生成有效页数的内置函数。 What you could do, is create temporary table: 您可以做的是创建临时表:

CREATE PROCEDURE dbo.GetSomeData
  @page INT,
  @size INT = 25
AS
BEGIN

  DECLARE @offset INT
  SET @offset = (@page - 1) * @size

  CREATE TABLE #temp
  (
    [Index] INT IDENTITY(1, 1) PRIMARY KEY,
    [Field1] VARCHAR(100),
    [Field2] VARCHAR(100),
    [Field3] VARCHAR(100)
  )

  INSERT INTO #temp ([Field1], [Field2], [Field3])
  SELECT [Field1], [Field2], [Field3] FROM SomeTable

  SELECT [Field1], [Field2], [Field3] FROM #temp WHERE [Index] BETWEEN @offset AND (@offset + @size)
END
GO

CREATE PROCEDURE dbo.GetCoutOfSomeData
AS
BEGIN
    DECLARE @count INT
    SELECT @count = COUNT([Field1]) FROM SomeTable

    SELECT @count
END
GO

Not terribly efficient, but you work with what you've got. 效率不高,但是您可以使用已有的东西。 Now, on the server side, you can create a method to get your results back, so, for example: 现在,在服务器端,您可以创建一个方法来取回结果,例如:

public class SomeDataSelector
{
  public IEnumerable<SomeDataType> GetSomeData(int page, int size)
  {
    List<SomeDataType> result = new List<SomeDataType>();
    using (SqlConnection conn = new SqlConnection(...)) {
      using (SqlCommand command = new SqlCommand("GetSomeData", conn)) {
        command.CommandType = CommandType.StoredProcedure;

        using (SqlDataReader reader = command.ExecuteReader()) {
          while (reader.Read()) {
            // Do work here to create instance of SomeDataType.


          }
        }
      }
    }

    return result;
  }

  public int GetCoutOfSomeData()
  {
    using (SqlConnection conn = new SqlConnection(...)) {
      using (SqlCommand command = new SqlCommand("GetSomeData", conn)) {
        command.CommandType = CommandType.StoredProcedure;

        int result = (int)command.ExecuteScalar();
      }
    }
  }
}

You can then bind that to your ASP.NET control. 然后,您可以将其绑定到ASP.NET控件。

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="SomeDataSelector" SelectMethod="GetSomeData" SelectCountMethod="GetCoutOfSomeData" />

<asp:GridView ID="GridView1" AutoGenerateColumns="True" DataKeyNames="Field1" DataSourceID="ObjectDataSource1" AllowPaging="True" PageSize="25" />

It's not a complete solution, but should be enough to get you going. 这不是一个完整的解决方案,但应该足以使您前进。

UPDATE A friend of mine found an alternative article about paging in Sql 2000: http://www.codeproject.com/KB/database/SQLServer2KPagingSorting.aspx 更新我的一个朋友在Sql 2000中找到了另一篇有关分页的文章: http : //www.codeproject.com/KB/database/SQLServer2KPagingSorting.aspx

在asp.net的博客“ 自定义分页类”中给出了完整的解决方案。

I posted this article in CodeProject a few years ago that explains in details on exactly how you can achieve that with complete source and example 几年前,我在CodeProject上发布了这篇文章,详细解释了如何使用完整的源代码和示例来实现该目标。

http://www.codeproject.com/KB/aspnet/GridViewNeatPaging.aspx http://www.codeproject.com/KB/aspnet/GridViewNeatPaging.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM