2009-08-04 81 views
2

嗨,我有一个100000行的数据表。现在我想以页面大小50的用户形式呈现我的数据。在asp.net中寻呼

什么是呈现它的最佳方法。我是否应该尊重数据主义?或者我可以实现我自己的选择查询获得50条记录每当我按下一个按钮?

在此先感谢

+0

您正在使用哪个数据库? – 2009-08-05 08:56:09

回答

1

为100000,这将是非常耗费时间从数据库中获取到数据集中的所有记录,然后将这些页面。相反,我会去在数据库存储过程/查询中实现分页。这样,一次只能在前端代码中检索50条记录,用户响应速度会更快。

+0

感谢您的快速回复。但这里的问题是从当前的事件中获得下一个和前50个记录。我怎样才能得到这些记录。你可以给我一个简单的查询来做到这一点 – Nagu 2009-08-04 10:06:20

+0

http://blogs.x2line.com/al/archive/2005/11/18/1323.aspx – Sekhat 2009-08-04 10:10:38

+0

你可以用LINQ to SQL- http:// www .dbtutorials.com/display/linq-to-sql-paging-cs.aspx – RichardOD 2009-08-04 10:34:31

0

那么“ListView”和“DataPager”呢?

0

我会使用pagedDataSource然后你可以绑定到一个中继器,数据列表或任何。

有和示例here

4

,如果你打开AllowPaging并设置PageSize至50 这将是可怕的低效你可以用一个GridView做到这一点很容易 - 每次移动到一个新的页面时,它会读取所有1 000 000行,计算出需要显示的50个,并将其余部分丢弃。

你想要的是你的数据库中的存储过程,它需要你想要显示的页码,找出该页面上的一组行并将它们返回给ASP.NET页面。如果您使用SQL Server 2005或更高你最好的选择是使用公用表表达式,所以你的存储过程会是这个样子(这是为Northwind数据库):

CREATE PROC [dbo].[PagedOrderList] 
@PageNumber INTEGER 
AS 
SET NOCOUNT ON 

DECLARE @lowerRecordNumber INTEGER 
DECLARE @upperRecordNumber INTEGER 

-- Work out the record numbers that bound the page 
SET @lowerRecordNumber = ((@pageNumber - 1) * 50) + 1 
SET @upperRecordNumber = (@pageNumber * 50); 

-- Create a CTE with all the records numbered 
WITH OrdersCTE ([RowNumber],[OrderId],[OrderDate],[RequiredDate],[ShippedDate], 
[CompanyName],[Value]) 
AS 
(
    SELECT 
    ROW_NUMBER() OVER(ORDER BY o.[OrderId]), 
    o.OrderID, 
    o.OrderDate, 
    o.RequiredDate, 
    o.ShippedDate, 
    c.CompanyName, 
    SUM(od.Quantity * od.UnitPrice) AS [Value] 
    FROM 
    Orders o INNER JOIN [Order Details] od ON o.OrderID = od.OrderID 
    INNER JOIN Customers c ON o.CustomerID = c.CustomerID 
    GROUP BY o.OrderID, o.OrderDate, o.RequiredDate, o.ShippedDate, c.CompanyName 
) 
-- Select the rows from the CTE that fall between the bounds we worked out 
SELECT * 
FROM OrdersCTE 
WHERE [RowNumber] BETWEEN @lowerRecordNumber AND @upperRecordNumber 

现在,回到你的页面。你需要放入一个DataGrid--他们比自定义分页支持更好 - 并且将AllowCustomPaging设置为True。你可能会发现有一种方法可以通过页码调用你的存储过程,然后你可以添加Previous,Next,First,Last,+10,-10按钮 - 无论你想要什么,只要找出页码并通过它的方法。

Private Sub loadData(ByVal pageNumber As Integer) 

    Dim orderDataTable As DataTable 
    'This uses the Microsoft Enterprise Library for data access 
    Dim DAL As Database 
    Dim cmd As DbCommand 

    DAL = DatabaseFactory.CreateDatabase("Northwind") 

    cmd = DAL.GetStoredProcCommand("PagedOrderList") 

    'Pass the page number to the stored proc 
    DAL.AddInParameter(cmd, "@pageNumber", DbType.Int32, pageNumber) 

    'Get a DataTable back with the 50 rows we want 
    orderDataTable = DAL.ExecuteDataSet(cmd).Tables(0) 

    'Bind the data to the grid 
    With OrderDataGrid 
     .DataSource = orderDataTable 
     .DataBind() 
     'Set the page number so we know where we are in the dataset 
     .CurrentPageIndex = pageNumber 
    End With 

End Sub 


Private Sub PreviousButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PreviousButton.Click 

    If OrderDataGrid.CurrentPageIndex = 0 Then 
     'Make sure we don't try to load a negative page number 
    Else 
     'Work out the previous page number and load the data for it 
     Call loadData(OrderDataGrid.CurrentPageIndex - 1) 
    End If 

End Sub 

Private Sub NextButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NextButton.Click 

    'Work out the nextpage number and load the data for it 
    Call loadData(OrderDataGrid.CurrentPageIndex + 1) 

End Sub