VB数据分页[转]

 Public   Sub   AbsolutePageX()   
          
        
Dim   rstEmployees   As   ADODB.Recordset   
        
Dim   strCnn   As   String   
        
Dim   strMessage   As   String   
        
Dim   intPage   As   Integer   
        
Dim   intPageCount   As   Integer   
        
Dim   intRecord   As   Integer   
    
        
'   使用客户端游标为雇员表打开一个记录集。   
        strCnn   =   "Provider=sqloledb;"   &   _   
              
"Data   Source=srv;Initial   Catalog=pubs;User   Id=sa;Password=;   "   
        
Set   rstEmployees   =   New   ADODB.Recordset   
        
'   使用客户端游标激活   AbsolutePosition   属性。   
        rstEmployees.CursorLocation   =   adUseClient   
        rstEmployees.Open   
"employee",   strCnn,   ,   ,   adCmdTable   
          
        
'   显示姓名和受雇日期,每次五个记录。   
        rstEmployees.PageSize   =   5   
        intPageCount   
=   rstEmployees.PageCount   
        
For   intPage   =   1   To   intPageCount   
              rstEmployees.AbsolutePage   
=   intPage   
              strMessage   
=   ""   
              
For   intRecord   =   1   To   rstEmployees.PageSize   
                    strMessage   
=   strMessage   &   _   
                          rstEmployees!fname   
&   "   "   &   _     
                          rstEmployees!lname   
&   "   "   &   _     
                          rstEmployees!hire_date   
&   vbCr   
                    rstEmployees.MoveNext   
                    
If   rstEmployees.EOF   Then   Exit   For   
              
Next   intRecord   
              
MsgBox   strMessage   
        
Next   intPage   
        rstEmployees.Close   
    
  
End   Sub   

AbsolutePage   属性   (ADO)  
                     
   
  指定当前记录所在的页。  
   
  设置和返回值  
   
  设置或返回从   1   到   Recordset   对象   (PageCount)   所含页数的长整型值,或者返回以下常量。  
   
  常量   说明    
  AdPosUnknown   Recordset   为空,当前位置未知,或者提供者不支持   AbsolutePage   属性。    
  AdPosBOF   当前记录指针位于   BOF(即   BOF   属性为   True)。    
  AdPosEOF   当前记录指针位于   EOF(即   EOF   属性为   True)。    
   
   
  说明  
   
  使用   AbsolutePage   属性可识别当前记录所在的页码。使用   PageSize   属性可将   Recordset   对象逻辑划分为一系列的页,每页的记录数等于   PageSize(最后一页除外,该页记录数较少)。提供者必须支持该属性的相应功能才能使用该属性。  
   
  与   AbsolutePosition   属性一样,AbsolutePage   从   1   开始并在当前记录为   Recordset   中的第一个记录时等于   1。设置该属性可移动到特定页的第一个记录。从   PageCount   属性中可获得总页数。  
   
  PageCount   属性   (ADO)  
                     
   
  指示   Recordset   对象包含的数据页数。  
   
  返回值  
   
  返回长整型值。  
   
  说明  
   
  使用   PageCount   属性可确定   Recordset   对象中数据的页数。“页”是大小等于   PageSize   属性设置的记录组。即使最后页不完整,由于记录数比   PageSize   值少,该页也会作为   PageCount   值中的附加页进行计数。如果   Recordset   对象不支持该属性,该值为   -1   以表明   PageCount   无法确定。  
   
  有关页的功能的详细信息,请参阅   PageSize   和   AbsolutePage   属性。  
   
  RecordCount   属性   (ADO)  
                     
   
  指示   Recordset   对象中记录的当前数目。  
   
  返回值  
   
  返回长整型值。  
   
  说明  
   
  使用   RecordCount   属性可确定   Recordset   对象中记录的数目。ADO   无法确定记录数时该属性返回   –1。读已关闭的   Recordset   上的   RecordCount   属性将产生错误。  
   
  如果   Recordset   对象支持近似定位或书签(即   Supports   (adApproxPosition)   或   Supports   (adBookmark)   各自返回   True),不管是否完全填充该值,该值将为   Recordset   中记录的精确数目。如果   Recordset   对象不支持近似定位,该属性可能由于必须对所有记录进行检索和计数以返回精确   RecordCount   值而严重消耗资源。  

SQL语句的实现

select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段

10 = 每页记录数

20 = (当前页 + 1) * 每页记录数

以上语句即可以实现分页,但是最后取出的结果排序是升序,如果需要结果集为降序(例如时间),则有两种方法可以处理

1.使用以下语句,但效率可能要降低一些

select * from 表名 b, (select top 10 主键字段,排序字段 from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a order by 排序字段 ) c where b.主键字段 = c.主键字段 order by c.排序字段 desc

2.在ado里处理,将记录集游标移到最后,然后前移

''以下为asp范例

set rsTemp = Server.CreateObject("adodb.recordset")

rsTemp.Open 语句,conn,1,1

rsTemp.MoveLast

for i = 1 to rsTemp.RecordCount

 '取值....

rsTemp.MovePrevious

next

经测试,以上分页方法比使用临时表分页速度还要快,并且简单易用



原文地址:https://www.cnblogs.com/sekihin/p/595302.html
Creative Commons License 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。