Vbscript 在经典ASP中处理ADODB连接
我是一个ASP.NETC的家伙,必须回到经典的ASP,并且需要一些帮助 首先,看看这两个函数(我知道在VBScript中,注释是由Vbscript 在经典ASP中处理ADODB连接,vbscript,asp-classic,adodb,Vbscript,Asp Classic,Adodb,我是一个ASP.NETC的家伙,必须回到经典的ASP,并且需要一些帮助 首先,看看这两个函数(我知道在VBScript中,注释是由'声明的,而不是由/声明的,但是这里的语法高亮显示会弄乱') 第一版: Function DoThing Dim Connection, Command, Recordset Set Connection = Server.CreateObject("ADODB.Connection") Set Command = Server.Creat
'
声明的,而不是由/
声明的,但是这里的语法高亮显示会弄乱'
)
第一版:
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
第二版:
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
或
?
问题#2:
在进行选择时,我应该使用断开连接的记录集,如
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
也调用垃圾收集器时。在销毁之前关闭它们)
问题#4:
我必须包括
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing
即使函数到此结束,并且这些对象超出范围,垃圾收集器(应该)也会处理它们
问题#5:
经典ASP是否有池连接,以便我可以用每个命令打开和关闭它们,还是应该传递一个公共连接对象
提前感谢您的帮助,Andrea。问题1。
两者都有。
这取决于您是否需要所有参数记录集。Open
有,也取决于您的个人偏好
问题#2。
你可以,但没关系。这几乎没有什么区别——您的命令已经完成,尽管连接对象已打开,但服务器上的所有锁和东西都已释放
问题#3。
VBScript中没有垃圾收集器,有引用计数
当不再引用对象时,对象将被销毁,并调用其所有终结器。
对于池连接,这可能会有所不同——因为服务器可能会保留对连接的引用,所以将变量设置为Nothing
实际上可能没有任何作用,因为引用计数不会达到零(您不必将其设置为Nothing
无论如何,它将在退出该功能时自动完成)
如果服务器不打算共用连接,它可能没有对它的引用,此时将变量设置为Nothing
(显式或隐式)也将关闭连接
我个人的偏好是调用Close
,这样我就知道我的东西在哪里了,但不要将变量设置为Nothing
。这在池连接和非池连接上都可以正常工作
问题4。
见问题3
问题5。
在web环境中,您总是希望在需要时打开一个新的连接对象,除非您的多个方法必须在同一事务中执行它们的位。对于StackOverflow上合理的VB注释,请在每个注释的末尾添加结束单引号。否则,对于代码格式化程序,它们只是未终止d字符串。为了补充一个优秀的答案,ASP确实有连接池。不过,这是我能找到的唯一一篇提到这一事实的Microsoft文章。这取决于你使用的光标,如果你使用的光标允许你在记录集中前后移动,那么连接需要保持打开,否则如果你试图向后走,你会得到一个ADODB错误。然而,如果你只需要在结果中向前导航,那么断开连接的记录集将在Cheran的链接中的“如何重新创建这些测试”部分中节省一些资源,它说cn(x).close'注释这一行以重新创建问题
,但GSerg说,Set cn(x)=Nothing
行应该足够了,因为它调用了连接。close
。哪一个是对的?@kappa如果服务器保留了对连接的引用,则链接是对的,而我是错的。但是,“使用Jet OLE DB提供程序和ODBC驱动程序的连接不是池连接”,所以在实践中我是正确的。我将修改我的答案,感谢您指出这一点。