【发布时间】: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