C#查詢結果學習筆記
通過C#查詢結果進行分頁就是以結果集的子集處理查詢結果的過程,這樣,每次返回給用戶的只是當前頁面的數(shù)據(jù)大小。
DataAdapter對象通過重載Fill方法提供了返回當前頁面數(shù)據(jù)的功能。然而,這種方法對大數(shù)據(jù)量的C#查詢結果并不是***的選擇,這是因為:當DataAdapter用請求的結果填充DataTable或者DataSet時,數(shù)據(jù)庫返回的資源仍是全部的查詢結果,只是在返回時附加了額外的限定條件才返回了少量的記錄集的。
要使用Fill方法返回當前一頁的記錄,需要指定開始記錄startRecord,和當前頁的***記錄數(shù)maxRecords。
下面的例子用來記錄C#查詢結果:
- usingSystem;
- usingSystem.Data;
- usingSystem.Data.SqlClient;
- usingSystem.Drawing;
- usingSystem.Windows.Forms;
- publicclassPagingSample:Form
- {
- //Form控件.
- ButtonprevBtn=newButton();
- ButtonnextBtn=newButton();
- staticDataGridmyGrid=newDataGrid();
- staticLabelpageLbl=newLabel();
- //分頁變量
- staticintpageSize=10;//要顯示的頁數(shù)
- staticinttotalPages=0;//總頁數(shù)
- staticintcurrentPage=0;//當前頁
- staticstringfirstVisibleCustomer="";
- //當前頁的***條記錄,用來進行移動“前一頁”的定位。
- staticstringlastVisibleCustomer="";
- //當前頁的***條記錄,用來進行移動“下一頁”的定位。
- //DataSet用來綁定到DataGrid.
- staticDataTablecustTable;
- //初始化連接和DataAdapter.
- staticSqlConnectionnwindConn=newSqlConnection
("DataSource=.;IntegratedSecurity=SSPI;InitialCatalog=northwind");- staticSqlDataAdaptercustDA=newSqlDataAdapter("",nwindConn);
- staticSqlCommandselCmd=custDA.SelectCommand;
【編輯推薦】