こんにちは、100000 行のデータを含むテーブルがあります。ここで、ページ サイズ 50 のユーザー フォームでデータを表示したいと考えています。
それを提示するための最良のアプローチは何ですか。データリストの方がいいですか? または、次のボタンを押すたびに 50 レコードを取得するための独自の選択クエリを実装できますか?
前もって感謝します
スイッチをオンにして 50AllowPaging
に設定すると、 GridViewを使用してこれを非常に簡単に行うことができます。表示し、残りを捨てる。PageSize
代わりに必要なのは、表示するページ番号を取得し、そのページの行のセットを処理して 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 を配置する必要があります (カスタム ページングのサポートが他より優れています)。True に設定AllowCustomPaging
します。ページ番号を指定してストアド プロシージャを呼び出す 1 つのメソッドを用意した方が簡単な場合があります。その後、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
asp.net のページング コントロールを作成しました。これは基本的なコントロールですが、役立つ場合があります。基本的なページャー コントロール
100000 の場合、すべてのレコードをデータベースからデータセットに取得してページングするには、非常に時間がかかります。代わりに、データベースのストアド プロシージャ/クエリにページングを実装します。そうすれば、フロントエンド コードで一度に 50 レコードしか取得できず、ユーザーの応答が速くなります。
pagedDataSource を使用すると、リピーター、データリストなどにバインドできます。
あります例はこちら。
「ListView」と「DataPager」はどうですか?