问专家-ADO的RECORDSET的RECORDCOUNT属性总是为-1

来源:百度文库 编辑:神马文学网 时间:2024/04/29 10:45:51
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就行啊。
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 数据库, database, query。
把这个问题推荐给朋友
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)