是否有必要在将 Adodb.recordset 对象设置为空之前关闭它? [英] Is it necessary to close an Adodb.recordset object before setting it to nothing?

查看:24
本文介绍了是否有必要在将 Adodb.recordset 对象设置为空之前关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim rs as ADODB.Recordset
set rs = ReturnARecordset 'assume ReturnARecordset does just that...

'do something with rs

rs.Close
set rs = Nothing

在设置为空之前是否需要调用 rs.Close?

Is it necessary to call rs.Close before setting it to nothing?

我们有一个全局连接,我们在应用程序期间保持打开状态,所有记录集对象都使用这个相同的连接.我在下面看到两个答案,讨论需要关闭记录集以确保连接不会保持打开状态.对我来说,这听起来像是很多愚蠢的谈话,因为连接是由连接对象控制的,而不是记录集对象,对吧?但是,如果我在这里遗漏了什么,请告诉我......

We have one global connection that we keep open for the duration of the application and all recordset objects use this same connection. I see two answers below talking about the need to close recordsets in order to ensure connections aren't left hanging open. To me that sounds like a lot of silly talk because connections are controlled with connection objects, not recordset objects right? But please let me know if I'm missing something here...

推荐答案

显式调用 Close 的唯一原因是当您不确定是否从项目中的其他位置引用记录集时,通常是一些草率编码的结果.

The only reason calling Close explicitly is when you are not sure if the recordset is referenced from somewhere else in your project, usually a result of some sloppy coding.

Dim rs as ADODB.Recordset
Set rs = ReturnARecordset
...
MyControl.ObscureMethod rs
...
Set rs = Nothing

最后一行应该在不显式调用 Close 的情况下终止记录集实例,除非 MyControl 持有额外的引用,从而阻止正常的拆除.在 rs 上调用 Close 将确保 MyControl 不能将其引用用于任何有用的事情,同时在火焰中崩溃.

Last line is supposed to terminate the recordset instance without calling Close explicitly, unless MyControl is holding an extra reference and thus preventing normal tear-down. Calling Close on rs will make sure MyControl cannot use its reference for anything useful, crashing in flames in the meantime.

这篇关于是否有必要在将 Adodb.recordset 对象设置为空之前关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆