高难:系统是否自动释放用Conn.Execute隐式创建的匿名RecordSet对象?

madpolice 2004-10-22 10:40:09
myVariable1 = Conn.Execute("SELECT COUNT(*) FROM myTable1")(0)

上面的语句会隐式创建一个RecordSet对象,并且这个对象是匿名的。
那么,这个对象如何来释放?
是系统自动释放还是需要程序员用某种办法来释放?
如果是系统自动释放,那何时被释放?
如果不能被释放,那这种写法是不是很不好?

请高手给个标准回答,谢谢!
...全文
202 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluemoon0001 2004-10-24
  • 打赏
  • 举报
回复
刚装完VS.net,上来看看,两年没用这个东西了,现在重新学起,呵呵...
bluemoon0001 2004-10-24
  • 打赏
  • 举报
回复
用类试一下,Vbscript同样可以使用类。
madpolice 2004-10-23
  • 打赏
  • 举报
回复
先给分,尽管问题还没解决。给分不一定合理,请大家谅解。

上面有几个朋友提到了“使用 Close 方法关闭 Connection 对象的同时,也将关闭与连接相关联的任何活动 Recordset 对象”,是的,微软也是这么解释的。

但是,问题是,

如何在关闭后将这些对象所占用的内存空间释放!!
对于Conneciton对象来说,可以释放,用Set Conn = Nothing就行了。
而RecordSet对象在这里是匿名的,也就是说程序员无法获得对此对象的引用,因此无法Set ?? = Nothing。

bluemoon0001(小天--五星级的菜鸟)提出了实际测试的思路,目前看也只能这样了。
欢迎其他人继续就此问题发表看法。
madpolice 2004-10-23
  • 打赏
  • 举报
回复
ADO 2.8 API Reference

Close Method
Closes an open object and any dependent objects.

Syntax
object.Close

Remarks
Use the Close method to close a Connection, a Record, a Recordset, or a Stream object to free any associated system resources. Closing an object does not remove it from memory; you can change its property settings and open it again later. To completely eliminate an object from memory, set the object variable to Nothing (in Visual Basic) after closing the object.

Connection

Using the Close method to close a Connection object also closes any active Recordset objects associated with the connection. A Command object associated with the Connection object you are closing will persist, but it will no longer be associated with a Connection object; that is, its ActiveConnection property will be set to Nothing. Also, the Command object's Parameters collection will be cleared of any provider-defined parameters.

You can later call the Open method to re-establish the connection to the same, or another, data source. While the Connection object is closed, calling any methods that require an open connection to the data source generates an error.

Closing a Connection object while there are open Recordset objects on the connection rolls back any pending changes in all of the Recordset objects. Explicitly closing a Connection object (calling the Close method) while a transaction is in progress generates an error. If a Connection object falls out of scope while a transaction is in progress, ADO automatically rolls back the transaction.

Recordset, Record, Stream

Using the Close method to close a Recordset, Record, or Stream object releases the associated data and any exclusive access you may have had to the data through this particular object. You can later call the Open method to reopen the object with the same, or modified, attributes.

While a Recordset object is closed, calling any methods that require a live cursor generates an error.

If an edit is in progress while in immediate update mode, calling the Close method generates an error; instead, call the Update or CancelUpdate method first. If you close the Recordset object while in batch update mode, all changes since the last UpdateBatch call are lost.

If you use the Clone method to create copies of an open Recordset object, closing the original or a clone does not affect any of the other copies.

hqppp 2004-10-22
  • 打赏
  • 举报
回复
还有没有答案
dama2003 2004-10-22
  • 打赏
  • 举报
回复
conn释放了,自然rs就没有了
madpolice 2004-10-22
  • 打赏
  • 举报
回复
aspgreener(水若寒):
朋友,没有rs。
aspgreener 2004-10-22
  • 打赏
  • 举报
回复
rs.close
set rs=nothing
madpolice 2004-10-22
  • 打赏
  • 举报
回复
xing_shou(漂) :
myVariable1不是对象。
madpolice 2004-10-22
  • 打赏
  • 举报
回复
xing_shou(漂):
首先感谢回贴,另外,
我想问的是如何释放RecordSet对象,而不是如何释放变量myVariable1。

xiaoxingchi(第007元素):
首先感谢回贴,另外,
我想问的是如何释放RecordSet对象,而不是如何释放Connection对象。
xing_shou 2004-10-22
  • 打赏
  • 举报
回复
那把 Conn 释放了,myVariablel 对象没有释放呀
xiaoxingchi 2004-10-22
  • 打赏
  • 举报
回复
Set Conn = Nothing
这样不可以吗?
xing_shou 2004-10-22
  • 打赏
  • 举报
回复
Set myVariablel=Nothing
myVariable=Empty
surferc 2004-10-22
  • 打赏
  • 举报
回复
gz
帮顶
bladeinside 2004-10-22
  • 打赏
  • 举报
回复
msdn

Close 方法 (ADO)


关闭打开的对象及任何相关对象。

语法

object.Close

说明

使用 Close 方法可关闭 Connection 对象或 Recordset 对象以便释放所有关联的系统资源。关闭对象并非将它从内存中删除,可以更改它的属性设置并且在此后再次打开。要将对象从内存中完全删除,可将对象变量设置为 Nothing。

Connection

使用 Close 方法关闭 Connection 对象的同时,也将关闭与连接相关联的任何活动 Recordset 对象。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
与正在关闭的 Connection 对象相关联的 Command 对象将被持久保留,但不再与 Connection 对象关联;即它的 ActiveConnection 属性将被设置为 Nothing。同时,Command 对象的 Parameters 集合将清除任何提供者定义的参数。
madpolice 2004-10-22
  • 打赏
  • 举报
回复
bluemoon0001(小天--五星级的菜鸟) :
问题是服务器有没有这种自己处理的机制呢?比如JAVA中的垃圾回收器。
这个有点像JAVA中的匿名内部类。
哪位高手还有关于此问题的解释?比如微软的文档等等。
bluemoon0001 2004-10-22
  • 打赏
  • 举报
回复
如果占内存就那就不要用这种写法了,你用这种写法,你找不到那块内存的地址,就肯定释放不了啦,只有等服务器自己处理了。
madpolice 2004-10-22
  • 打赏
  • 举报
回复
cdsun() :
你好,myVariable1不是对象!

bluemoon0001(小天--五星级的菜鸟) :
思路很好。不过我需要的是一个标准答案,如果发现占内存那如何解决?
zfd555 2004-10-22
  • 打赏
  • 举报
回复
各们说的都有道理! bluemoon0001(小天--五星级的菜鸟)最有棒
bluemoon0001 2004-10-22
  • 打赏
  • 举报
回复
唉,写个程序试一下了:
for i=1 to 10000
myVariable1 = Conn.Execute("SELECT COUNT(*) FROM myTable1")(0)
next

这样刷几下看看你机子的内存如何???我认为不会占内存。
加载更多回复(2)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧