【问题标题】:Handling ADODB connections in classic ASP在经典 ASP 中处理 ADODB 连接
【发布时间】:2011-11-19 13:27:53
【问题描述】:

我是一名 ASP.NET C# 人,必须回到经典 ASP,需要一些帮助。

首先,看看这两个函数(我知道在 VBScript 中 cmets 是由 ' 声明的,而不是由 // 声明的,但这里的语法高亮显示与 ' 混淆了)。

第一版:

Function DoThing

    Dim Connection, Command, Recordset

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")
    Set Recordset = Server.CreateObject("ADODB.Recordset")

    Connection.Open "blah blah blah"
    Command.CommandText = "blah blah blah"
    Recordset.Open Command, Connection

    If Recordset.EOF = False Then

        While Recordset.EOF = False Then

            // Do stuff.

            Recordset.MoveNext

        WEnd

    Else

        // Handle error.

    End If

    Recordset.Close
    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

第二版:

Function DoAnotherThing

    Dim Connection, Command, Recordset

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")

    Connection.Open "blah blah blah"

    Command.ActiveConnection = Connection
    Command.CommandText = "blah blah blah"

    Set Recordset = Command.Execute

    If Recordset.EOF = False Then

        While Recordset.EOF = False Then

            // Do stuff.

            Recordset.MoveNext

        WEnd

    Else

        // Handle error.

    End If

    Recordset.Close
    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

现在,让我们从问题开始:

问题 #1:

什么是最好的,

Connection.Open "blah blah blah"
Command.CommandText = "blah blah blah"
Recordset.Open Command, Connection

Connection.Open "blah blah blah"

Command.ActiveConnection = Connection
Command.CommandText = "blah blah blah"

Set Recordset = Command.Execute

?

问题 #2:

在执行 SELECT 时,我应该使用断开连接的记录集,例如

Set Recordset = Command.Execute

Command.ActiveConnection = Nothing

Connection.Close

Set Connection = Nothing

?

问题 #3:

我需要打电话吗

Recordset.Close
Connection.Close

虽然我有

Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing

就在下面(即垃圾收集器在被Set Stuff= Nothing 调用时也被.Close 他们在销毁之前)?

问题 #4:

我必须包括

Recordset.Close
Connection.Close

Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing

即使函数在那里结束,并且这些对象超出范围,并且垃圾收集器(应该)处理它们?

问题 #5:

经典 ASP 是否有池连接,所以我可以用每个命令打开和关闭它们,还是应该传递一个公共连接对象?


提前感谢您的帮助,安德里亚。

【问题讨论】:

  • 对于 StackOverflow 上合理的 VB cmets,在每条评论的末尾添加右单引号。否则对于代码格式化程序,它们只是未终止的字符串。

标签: vbscript asp-classic adodb


【解决方案1】:

问题 #1。

两者。
这取决于您是否想要Recordset.Open 拥有的所有参数,也取决于您的个人喜好。

问题 #2。

可以,但没关系。几乎没有什么区别——你的命令已经完成,虽然连接对象打开了,但所有的锁和东西都在服务器上释放了。

问题 #3。

VBScript 中没有垃圾收集器,只有引用计数。

当没有更多对它的引用时,对象将被销毁,并调用其所有终结器。
在池连接的情况下,这可能会有所不同——因为服务器可能保留对连接的引用,将变量设置为 Nothing 实际上可能不会做任何事情,因为引用计数不会达到零(而且你没有无论如何设置它为Nothing,它会在退出函数时自动完成。

如果服务器不打算池化连接,它可能不会引用它,此时将变量设置为Nothing(显式或隐式)也会关闭连接。

我个人的偏好是调用Close,以便我知道我的东西在哪里,但不要将变量设置为Nothing。这在池连接和非池连接上都可以正常工作。

问题 #4。

见问题 3。

问题 #5。

在 Web 环境中,您总是希望在需要时打开一个新的 Connection 对象,除非您的多个方法必须在同一个事务中执行它们的位。

【讨论】:

  • 只是为了补充一个很好的答案,ASP 确实有连接池。不过,KB article 是我能找到的唯一一篇提到这一事实的 Microsoft 文章。
  • 对 Q2 的回答取决于您使用的光标,如果您使用的光标允许您在记录集中前后移动,则连接需要保持打开状态,否则您如果您尝试向后退,会收到 ADODB 错误。而如果您只需要在结果中向前导航,那么断开连接的记录集将节省一些资源
  • 在 Cheran 链接的 如何重新创建这些测试 部分中,它显示 cn(x).close 'comment this line out to recreate the problem,但 GSerg 说 Set cn(x) = Nothing 行就足够了,因为它调用了 Connection.Close。哪个是对的?
  • @kappa 如果服务器保留对连接的引用,则链接是对的,我错了。但是,“使用 Jet OLE DB 提供程序和 ODBC 驱动程序的连接没有被汇集”,所以实际上我是正确的。我会修改我的答案,感谢您指出这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2023-03-09
  • 2020-10-24
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多