在VB学习中recordSet.RecordCount返回值为-1的解决办法

利用ADO连接数据库时,Rs.recordcount总是返回-1,可能的解决办法如下:在记录集打开前加上rs.cursorlocation=3 

rs.CursorLocation=3 是什么意思  

3 代表rs.CursorLocation = adUseClient 
就是代表使用客户端光标,和他对应的是 rs.CursorLocation = adUseServer 服务端光标 
--------------------------- 
CursorLocation 属性 

设置或返回游标服务的位置。 

设置和返回值 

设置或返回可设置为以下某个常量的长整型值。 

常量 说明 
adUseNone 没有使用游标服务。(该常量已过时并且只为了向后兼容才出现)。 
adUseClient 使用由本地游标库提供的客户端游标。本地游标服务通常允许使用的许多功能可能是驱动程序提供的游标无法使用的,因此使用该设置对于那些将要启用的功能是有好处的。AdUseClient 具有向后兼容性,也支持同义的 adUseClientBatch。 
adUseServer 默认值。使用数据提供者的或驱动程序提供的游标。这些游标有时非常灵活,对于其他用户对数据源所作的更改具有额外的敏感性。但是,Microsoft Client Cursor Provider(如已断开关联的记录集)的某些功能无法由服务器端游标模拟,通过该设置将无法使用这些功能。 

说明 

该属性允许在可用于提供者的各种游标库中进行选择。通常,可以选择使用客户端游标库或位于服务器上的某个游标库。 

该属性设置仅对属性已经设置后才建立的连接有影响。更改 CursorLocation 属性不会影响现有的连接。 

对于 Connection 或关闭的 Recordset 该属性为读/写,而对打开的 Recordset 该属性为只读。 

由 Execute 方法返回的游标继承该设置。Recordset 将自动从与之关联的连接中继承该设置。 

远程数据服务用法 当用于客户端 (ADOR) Recordset 或 Connection 对像时,只能将 CursorLocation 属性设置为 adUseClient。  

注:

使用RecordCount属性可确定Recordset对像中记录的数目。ADO无法确定记录数时,或者如果提供者或游标类型不支持RecordCount,则该属性返回–1。读已关闭的Recordset上的RecordCount属性将产生错误。 
如果Recordset对像支持近似定位或书签(即Supports(adApproxPosition)或Supports(adBookmark)各自返回True),不管是否完全填充该值,该值将为Recordset中记录的精确数目。如果Recordset对像不支持近似定位,该属性可能由于必须对所有记录进行检索和计数以返回精确RecordCount值而严重消耗资源。 
Recordset对象的游标类型会影响是否能够确定记录的数目。对仅向前游标,RecordCount属性将返回-1,对静态或键集游标返回实际计数,对动态游标取决于数据源返回-1或实际计数。

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">1</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/xifeijian">
                    <img src="https://profile.csdnimg.cn/6/1/3/3_xifeijian" class="avatar_pic" username="xifeijian">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/7.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/xifeijian" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">席飞剑</a></span>
                                                    <span class="flag expert">
                                <a href="https://blog.csdn.net/home/help.html#classicfication" target="_blank">
                                    <svg class="icon" aria-hidden="true">
                                        <use xlink:href="#csdnc-blogexpert"></use>
                                    </svg>
                                    博客专家
                                </a>
                            </span>
                                            </div>
                    <div class="text"><span>发布了390 篇原创文章</span> · <span>获赞 785</span> · <span>访问量 510万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://bbs.csdn.net/topics/395531972" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-messageboard">他的留言板
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    

这里是引用

猜你喜欢

转载自blog.csdn.net/missingtheway/article/details/104854414