分页测试

最近做了一个分页的小系统,但在分页过程中,不能够真正的移动页面记录。不知道怎么办,后来就语句实现,写了存储过程,真正实现了分布。存储过程如下,

create proc SqlPager
@sqlstr nvarchar(4000),--查询字符串
@currentpage int,--第N页
@pagesize int,--每页行数
@pagecount int output --总页数
as
set nocount on
declare @P1 int,--P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
 select @pagecount = cast(ceiling(1.0*@rowcount/@pagesize) as int)--as 总页数--,@rowcountas总行数,@currentpageas当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
go

 

分别在参数中传递它们的查询字条串,当前页及每页的记录数。

以下是查询分页方法的事件

if not ADOConnection1.Connected  then exit;
  if Trim(Memo1.text) ='' then exit;
   if page= 0 then page:=1;
  //存储过程调用;
  adostoredproc1.Close;
  adostoredproc1.Parameters.ParamByName('@sqlstr').Value:=memo1.Text;
  adostoredproc1.Parameters.ParamByName('@currentpage').Value:=page;
  adostoredproc1.Parameters.ParamByName('@pagesize').Value:=10;
  adostoredproc1.ExecProc;
  adostoredproc1.Open;
  pages:=adostoredproc1.Parameters.parambyname('@pagecount').Value;
  label6.Caption:=inttostr(pages);
  recordsaffected:=0;
  //datasource2.DataSet:=adodataset1;
  //adodataset1.Open;
  adodataset1.Recordset:=adostoredproc1.Recordset.NextRecordset(recordsaffected);
  datasource2.DataSet:=adodataset1;
  adodataset1.Open;
  label13.Caption:=inttostr(page);

最初执行存储过程,得到的是两个记录集,但第一个记录集为空,不知道怎么取下一个记录集。最后发现ADOSTOREDPROC有一个NEXTRECORDSET()的一个方法。所以就可以得到下一个记录集了。但要把下一个记录集赋给另一个记录集变量。再打开新的记录集变量,就可以了。一切OK!但有一点要注意的是在NEXTRECORDSET()这个里面必需是一个变量。否则是不能通过编译的。可以定义 RecordsAffected:OleVariant;来做下一个记录集的变量标。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值