vba rs.close 与 rs = 没有在 RecordSet 中有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2542449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats the difference between rs.close vs rs = nothing in a RecordSet
提问by Curtis Inderwiesche
I often find it confusing as to when it is appropriate to use:
我经常发现什么时候适合使用令人困惑:
rs.Close
opposed to
反对
Set rs = Nothing
I can understand needing to close a connection to a source, but should I be using both when the variable falls out of scope?
我可以理解需要关闭与源的连接,但是当变量超出范围时,我应该同时使用两者吗?
Can I just set the variable to Nothing in order to skip the step of Closing the connection? Would this be considered a bad practice?
我可以将变量设置为 Nothing 以跳过关闭连接的步骤吗?这会被认为是一种不好的做法吗?
采纳答案by Jojo Sardez
By using the "Close" method you are closing the connection to the database but is still in the memory where you can open again using the "Open" method.
通过使用“关闭”方法,您正在关闭与数据库的连接,但仍在内存中,您可以使用“打开”方法再次打开。
Setting the recordset to "Nothing" on the other hand releases the object completely from the memory.
另一方面,将记录集设置为“Nothing”从内存中完全释放对象。
回答by David-W-Fenton
The Closemethod tears down the memory structure.
该Close方法拆除内存结构。
Setting the variable to Nothingclears the pointer to that memory structure.
将变量设置为Nothing清除指向该内存结构的指针。
Theoretically, clearing the pointer should release the memory the pointer was referring to, because VBA uses reference counting for determining when it can release memory. Unfortunately, various things can go wrong and the reference count can end up out of whack, and memory won't be released even when it should be.
理论上,清除指针应该释放指针所指的内存,因为 VBA 使用引用计数来确定何时可以释放内存。不幸的是,各种各样的事情可能会出错,引用计数可能会失控,即使应该释放内存也不会释放。
Thus, to be sure you're not subject to memory leaks, or the weird kinds of bugs caused by implicit and unreleased references, you both Closeand set to Nothing.
因此,为了确保您不会受到内存泄漏或由隐式和未释放引用引起的奇怪类型的错误的影响,您都Close将Nothing.
回答by jurev
You can set Recordset to Nothing without needing to call Close, according to official documentation:
根据官方文档,您可以将 Recordset 设置为 Nothing,而无需调用 Close:
An alternative to the Close method is to set the value of an object variable to Nothing (Set dbsTemp = Nothing).
Close 方法的替代方法是将对象变量的值设置为 Nothing(Set dbsTemp = Nothing)。
More info: Recordset.Close Method (DAO)
回答by Edward
Well, in my own experience, if the recorset object(hereinafter referred to as "RS") is declared locally(within the function/procedure, hereinafter referred to as "B") and will not be delivered out to where B is called(hereinafter referred to as "A"), it's safe and suggested to close and set RS to nothing inside B; but in the following situations:
好吧,以我自己的经验,如果recorset对象(以下简称“RS”)在本地(在函数/过程中,以下简称“B”)声明并且不会传递到B被调用的地方(以下简称“A”),是安全的,建议在B内部关闭RS并设置为空;但在以下情况下:
- RS is delivered as one of the parmeter argument of B(either byval or byref) from A
- RS is declared in B and is to be one of the return value(s) of B for A to use
- RS 作为来自 A 的 B(byval 或 byref)的参数参数之一传递
- RS 在 B 中声明,并且是 B 的返回值之一供 A 使用
RS in B should only set to nothing without closing it, orelse the recordset object returned to A(or sent to B as one of the parameter(s) from A) will also be closed and set nothing, making it inaccessible in A, even if you returned RS to A in advance and then close in B!
B 中的 RS 应该只设置为空而不关闭它,否则返回到 A(或作为 A 中的参数之一发送到 B)的记录集对象也将被关闭并且不设置任何内容,使其在 A 中无法访问,即使如果您提前将 RS 返回到 A,然后在 B 中关闭!

