3

SO, I learned that in old school ASP Classic if you use the

<object runat="server" id="somename" progid="ADODB.Recordset"></object>

tag it provides a performance improvement over using

set rs = Server.CreateObject("ADODB.Recordset")

However, all was lost when I tried to execute. set rs = nothing Is this the intended behavior? does that tag make asp handle disposing of object better?


If you are including this object declaration directly in your asp page (not global.asax) then it terminates itself once the page request ends.

You can also include these in your global.asa file if you want them to persist over the session or application scope.

<OBJECT ID=rsCustomers PROGID="ADODB.Recordset" RUNAT="Server" SCOPE="Application"></OBJECT>

Note the SCOPE attribute. This loads the recordset into an application variable (can be set to either "session" or "application" depending on your use case). You can easily destroy session variables in your scripts like this:

Session.Abandon

Or if you want to kill the session variable but leave the session open, just terminate the session variable like this:

Set Session("rsCustomers") = Null

If you just want the RS object for the page, then leave out the scope attribute, and include the object declaration in the page itself.

The id will be available as a regular variable as though you declared it in a vbscript codeblock directly in the page. Just remember to do a .close to close the object, and the object will terminate on its own as soon as the page load is complete (e.g. when the page scope is complete).

Hope this helps.

4

1 回答 1

1

如果您将这个对象声明直接包含在您的 asp 页面(不是 global.asax)中,那么它会在页面请求结束后自行终止。

如果您希望它们在会话或应用程序范围内持续存在,您还可以将它们包含在您的 global.asa 文件中。

<OBJECT ID=rsCustomers PROGID="ADODB.Recordset" RUNAT="Server" SCOPE="Application"></OBJECT>

注意SCOPE属性。这会将记录集加载到应用程序变量中(可以根据您的用例设置为“会话”或“应用程序”)。您可以像这样轻松地销毁脚本中的会话变量:

Session.Abandon

或者,如果您想终止会话变量但保持会话打开,只需像这样终止会话变量:

Set Session("rsCustomers") = Null

如果您只想要页面的 RS 对象,则省略范围属性,并在页面本身中包含对象声明。

id 将作为常规变量提供,就像您直接在页面中的 vbscript 代码块中声明它一样。只需记住.close关闭对象,一旦页面加载完成(例如,当页面范围完成时),对象将自行终止。

希望这可以帮助。

于 2012-11-02T13:31:52.240 回答