任何拥有分页示例代码链接的人都可以更改为在(10000+记录)表中工作?我想在我的asp.net页面上显示记录。
干杯
答案 0 :(得分:5)
分页的最佳做法是在存储过程中执行,因此您可以限制撤回的数据量,例如:
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
这是特定于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 ;
编辑,好像您正在使用Sql 2000。
分页在Sql 2000上有点棘手,因为您没有任何内置函数来生成有效的页数。您可以做的是创建临时表:
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
效率不是很高,但你可以使用你所拥有的东西。现在,在服务器端,您可以创建一个方法来获取结果,例如:
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();
}
}
}
}
然后,您可以将其绑定到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" />
这不是一个完整的解决方案,但应该足以让你前进。
更新我的一位朋友在Sql 2000中发现了一篇关于分页的替代文章:http://www.codeproject.com/KB/database/SQLServer2KPagingSorting.aspx
答案 1 :(得分:1)
我已在我的博客Custom Pagination Class in asp.net中提供了完整的解决方案...请告诉我更多详情....
答案 2 :(得分:1)
几年前我在CodeProject中发布了这篇文章,详细解释了如何通过完整的源代码和示例来实现这一目标
http://www.codeproject.com/KB/aspnet/GridViewNeatPaging.aspx