我需要用DBGRID分页显示数据库中的数据,是用ADOQUERY连接数据库,其中ADOQUERY的sql语句如下:
adoq.sql.add('select top '+inttostr(per_page_count)+' * from table1 where id not in (select top '+inttostr(PrevPageVariant)+' id from table1)')  per_page_count:integer;//每页要显示的记录条数,全局变量
  PrevPageVariant:integer;//每页的第一条记录,全局变量
  table1是一张表名,id是这张表的一个主键。有4个按钮“第一页”“上一页”“下一页”“末页”,请问这四个按钮的编程要怎样实现?

解决方案 »

  1.   

    declare @SQLStr varchar(8000)
    set @SQLStr='SELECT Top '+cast(@每页大小 as varchar)+' * FROM 表 WHERE 主键列 NOT IN (SELECT TOP '+cast(@每页大小*(@第几页-1) as varchar)+' 主键列 from 表 )'
    exec(@SQLStr)自己再改改。
      

  2.   

    对于楼主写的
    第一页
    adoq.sql.add('select top '+inttostr(per_page_count)+' * from table1 where id not in (select top '+inttostr(PrevPageVariant)+' id from table1)')
    PrevPageVariant:=0
    adoq.open;下一页
    adoq.sql.add('select top '+inttostr(per_page_count)+' * from table1 where id not in (select top '+inttostr(PrevPageVariant)+' id from table1)')
    inc(PrevPageVariant,1);
    adoq.open;上一页
    adoq.sql.add('select top '+inttostr(per_page_count)+' * from table1 where id not in (select top '+inttostr(PrevPageVariant)+' id from table1)')
    dec(PrevPageVariant,1);
    adoq.open;末页
    adoq.sql.add('select count(*) as 总数 from table1);
    adoq.open;
    PrevPageVariant:=trunc(adoq.fieldbyname('总数').asinteger/per_page_count);
    adoq.sql.add('select top '+inttostr(per_page_count)+' * from table1 where id not in (select top '+inttostr(PrevPageVariant)+' id from table1)')
    adoq.open;