C#查询结果学习笔记

通过C#查询结果进行分页就是以结果集的子集处理查询结果的过程,这样,每次返回给用户的只是当前页面的数据大小。

DataAdapter对象通过重载Fill方法提供了返回当前页面数据的功能。然而,这种方法对大数据量的C#查询结果并不是***的选择,这是因为:当DataAdapter用请求的结果填充DataTable或者DataSet时,数据库返回的资源仍是全部的查询结果,只是在返回时附加了额外的限定条件才返回了少量的记录集的。

要使用Fill方法返回当前一页的记录,需要指定开始记录startRecord,和当前页的***记录数maxRecords。

下面的例子用来记录C#查询结果:

 

 
 
 
  1. usingSystem;  
  2. usingSystem.Data;  
  3. usingSystem.Data.SqlClient;  
  4. usingSystem.Drawing;  
  5. usingSystem.Windows.Forms;  
  6.  
  7. publicclassPagingSample:Form  
  8. {  
  9. //Form控件.  
  10. ButtonprevBtn=newButton();  
  11. ButtonnextBtn=newButton();  
  12.  
  13. staticDataGridmyGrid=newDataGrid();  
  14. staticLabelpageLbl=newLabel();  
  15.  
  16. //分页变量  
  17. staticintpageSize=10;//要显示的页数  
  18. staticinttotalPages=0;//总页数  
  19. staticintcurrentPage=0;//当前页  
  20. staticstringfirstVisibleCustomer="";  
  21. //当前页的***条记录,用来进行移动“前一页”的定位。  
  22. staticstringlastVisibleCustomer="";  
  23. //当前页的***条记录,用来进行移动“下一页”的定位。  
  24.  
  25. //DataSet用来绑定到DataGrid.  
  26. staticDataTablecustTable;  
  27.  
  28. //初始化连接和DataAdapter.  
  29. staticSqlConnectionnwindConn=newSqlConnection
    ("DataSource=.;IntegratedSecurity=SSPI;InitialCatalog=northwind");  
  30. staticSqlDataAdaptercustDA=newSqlDataAdapter("",nwindConn);  
  31. staticSqlCommandselCmd=custDA.SelectCommand; 

【编辑推荐】

  1. 如何用C#和ADO.NET访问
  2. 浅析C# Switch语句
  3. C#验证输入方法详解
  4. 简单介绍C# 匿名方法
  5. C# FileSystemWatcher对象
THE END