ADO的RECORDSET的RECORDCOUNT属性总是为-1

ADO的RECORDSET的RECORDCOUNT属性总是为-1
编号:QA002032  
建立日期:1999年11月14日最后修改日期:2004年1月2日
所属类别:Visual Basic - 数据库
  
    操作系统: nt
    编程工具: vb6,sql server
    问题: 请问如何判断ADO的RECORDSET为空?用RECORDCOUNT属性好象无论空否,值总是为-1。(mj)
  
    参考微软的Knowledge Base的文章:“Q194973 PRB: ADO: Recordcount May Return -1”,下面是该文中文译文:
    

    现象
    ====
    当在服务器端请求RecordCoun时会返回-1。这是因为ActiveX Data Objects (ADO) 2.0中的CursorType是adOpenForwardonly或者adOpenDynamic。如果是ADO 1.5,只发生在cursortype是adOpenForwardonly的时候。如果使用OLEDB provider for JET和SQL Server产生的结果可能不同,这依赖于数据库的提供者。
    提供者可能不支持某些CursorTypes。当你选择的CursorType不被支持时,提供者将选择最接近于你所请求的CursorType。请参考你的提供者的文档。此外,请注意不是所有的LockType和CursorType的组合都可以同时工作。改变LockType将强制改变CursorType。请确定使用调试来检查CursorType的值。
    
    原因
    =====
    
    在动态的游标中纪录号可能改变。Forward only的游标无法返回RecordCount。
    
    解决办法
    ==========
    使用adOpenKeyset(=1)或者adOpenStatic(=3)作为服务器端游标或者客户端游标。客户端只使用adOpenStatic作为CursorTypes,而不管你选择什么样的CursorType。
    
    状态
    ======
    
    这个形式是设计决定的。
    
    更多信息
    ================
    
    重复行为的步骤
    ---------------------------
    
    1. Open a standard .exe project in Visual Basic. From the Project menu, choose References. Select either the Microsoft Active Data Object 1.5 Library or the Microsoft Active Data Object 2.0 Library.
    
    2. Paste the following code in the form code window:
    
     Option Explicit
     Dim rs As ADODB.Recordset
    
     Private Sub Form_Load()
     'set up rs
     Set rs = New ADODB.Recordset
     rs.CursorLocation = adUseServer
     rs.Open "Select ProductID from products", & _
     "Provider=Microsoft.Jet.OLEDB.3.51;" & _
     "Data Source=d:/vb5_win95/nwind.mdb", _
     adOpenDynamic, adLockUnspecified
    
     Debug.Print rs.RecordCount
     End Sub
    
     3. Replace the preceding Data Source with a Data Source on your computer. Run the preceding form and note the record count. Change the CursorType to adOpenForwardonly and note the record count.
    
     4. Change the CursorLocation to adUseClient and experiment with the different CursorTypes. In all cases the correct record count returns.
    

    
    陈晓鸣的意见:
    在打开recordset记录集的时候,如果参数3(就是CursorType)为键盘索引或者向前索引时(CursorType参数为0 or 1),为了提高处理速度,recordset纪录是一条一条给你的,而不是一下子都给你的。解决方法是使用动态或静态索引(CursorType参数为2 or 3)
    
    姚来存的意见:
    因为在c/s结构中,记录集是分页存储的,当你从服务器请求数据时,它不会给你全部数据。最好用rs.getrows()的到所有记录行
    
    杨晓峰、梦小凡的意见:
    将ADO连接的CursorLocation属性设置为客户端:rs.CursorLocation = adUseClient就行了。
    
    宋传真的意见:
    如果只是要判断ADO的RECORDSET为空,用RECORDSET.EOF=TRUE就行了!我从不用RECORDSETCOUNT=0判断是否为空!
    
    伊冰山的意见:
    如果ADO的RECORDCOUNT为-1,只要设置CursorType参数为2 or 3就行啊。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值