C#查詢結(jié)果學(xué)習(xí)筆記
通過C#查詢結(jié)果進(jìn)行分頁就是以結(jié)果集的子集處理查詢結(jié)果的過程,這樣,每次返回給用戶的只是當(dāng)前頁面的數(shù)據(jù)大小。
DataAdapter對(duì)象通過重載Fill方法提供了返回當(dāng)前頁面數(shù)據(jù)的功能。然而,這種方法對(duì)大數(shù)據(jù)量的C#查詢結(jié)果并不是***的選擇,這是因?yàn)椋寒?dāng)DataAdapter用請(qǐng)求的結(jié)果填充DataTable或者DataSet時(shí),數(shù)據(jù)庫返回的資源仍是全部的查詢結(jié)果,只是在返回時(shí)附加了額外的限定條件才返回了少量的記錄集的。
要使用Fill方法返回當(dāng)前一頁的記錄,需要指定開始記錄startRecord,和當(dāng)前頁的***記錄數(shù)maxRecords。
下面的例子用來記錄C#查詢結(jié)果:
- 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;//當(dāng)前頁
- staticstringfirstVisibleCustomer="";
- //當(dāng)前頁的***條記錄,用來進(jìn)行移動(dòng)“前一頁”的定位。
- staticstringlastVisibleCustomer="";
- //當(dāng)前頁的***條記錄,用來進(jìn)行移動(dòng)“下一頁”的定位。
- //DataSet用來綁定到DataGrid.
- staticDataTablecustTable;
- //初始化連接和DataAdapter.
- staticSqlConnectionnwindConn=newSqlConnection
("DataSource=.;IntegratedSecurity=SSPI;InitialCatalog=northwind");- staticSqlDataAdaptercustDA=newSqlDataAdapter("",nwindConn);
- staticSqlCommandselCmd=custDA.SelectCommand;
【編輯推薦】