Vbscript 经典ASP断开连接记录集问题
所以,我被要求更新一个旧的经典ASP网站。它没有使用参数化查询,输入验证也很少。为了简化操作,我编写了一个helper函数,它可以打开与数据库的连接,使用任何参数设置命令对象,并创建一个断开连接的记录集[我想!?!:)]以下是代码:Vbscript 经典ASP断开连接记录集问题,vbscript,asp-classic,recordset,Vbscript,Asp Classic,Recordset,所以,我被要求更新一个旧的经典ASP网站。它没有使用参数化查询,输入验证也很少。为了简化操作,我编写了一个helper函数,它可以打开与数据库的连接,使用任何参数设置命令对象,并创建一个断开连接的记录集[我想!?!:)]以下是代码: Function GetDiscRS(DatabaseName, SqlCommandText, ParameterArray) 'Declare our variables Dim discConn Dim discCmd Dim
Function GetDiscRS(DatabaseName, SqlCommandText, ParameterArray)
'Declare our variables
Dim discConn
Dim discCmd
Dim discRs
'Build connection string
Dim dbConnStr : dbConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & rootDbPath & "\" & DatabaseName & ".mdb;" & _
"Persist Security Info=False"
'Open a connection
Set discConn = Server.CreateObject("ADODB.Connection")
discConn.Open(dbConnStr)
'Create a command
Set discCmd = Server.CreateObject("ADODB.Command")
With discCmd
Set .ActiveConnection = discConn
.CommandType = adCmdText
.CommandText = SqlCommandText
'Attach parameters to the command
If IsArray(ParameterArray) Then
Dim cnt : cnt = 0
For Each sqlParam in ParameterArray
discCmd.Parameters(cnt).Value = sqlParam
cnt = cnt + 1
Next
End If
End With
'Create the Recordset object
Set discRs = Server.CreateObject("ADODB.Recordset")
With discRs
.CursorLocation = adUseClient ' Client cursor for disconnected set
.LockType = adLockBatchOptimistic
.CursorType = adOpenForwardOnly
.Open discCmd
Set .ActiveConnection = Nothing ' Disconnect!
End With
'Return the Recordset
Set GetDiscRS = discRS
'Cleanup
discConn.Close()
Set discConn = Nothing
discRS.Close() ' <=== Issue!!!
Set discRs = Nothing
Set discCmd = Nothing
End Function
函数GetDiscRS(数据库名、SqlCommandText、ParameterArray)
'声明我们的变量
暗盘
Dim discCmd
暗盘
'生成连接字符串
Dim dbConnStr:dbConnStr=“Provider=Microsoft.Jet.OLEDB.4.0;”_
“数据源=”&rootDbPath&“\”&DatabaseName&“.mdb;”和_
“持久化安全信息=False”
'打开连接
设置discConn=Server.CreateObject(“ADODB.Connection”)
discConn.Open(dbConnStr)
'创建命令
设置discCmd=Server.CreateObject(“ADODB.Command”)
使用discCmd
Set.ActiveConnection=discConn
.CommandType=adCmdText
.CommandText=SqlCommandText
'将参数附加到命令
如果是IsArray(参数数组),则
尺寸cnt:cnt=0
对于ParameterArray中的每个sqlParam
discCmd.Parameters(cnt).Value=sqlParam
cnt=cnt+1
下一个
如果结束
以
'创建记录集对象
Set discRs=Server.CreateObject(“ADODB.Recordset”)
用圆盘
.CursorLocation=adUseClient“断开连接的集合的客户端游标
.LockType=adlockBatch
.CursorType=adOpenForwardOnly
.Open discCmd
Set.ActiveConnection=Nothing'断开连接!
以
'返回记录集
设置GetDiscRS=discRS
"清理",
discConn.Close()
设置discConn=无
discRS.Close()“在您的代码中,我可以看到:
Set .ActiveConnection = Nothing ' Disconnect!
那么,此记录集尚未关闭?在您的代码中,我可以看到:
Set .ActiveConnection = Nothing ' Disconnect!
因此,此记录集尚未关闭?据我所知,断开连接的记录集是指手动填充的记录集,而不是从数据库填充的记录集,例如用作多维数组或某种哈希表 所以,您所拥有的并不是一个断开连接的记录集,因为它是从数据库填充的,通过处理它的连接,您只会导致代码无法正常工作 由于代码中已有
Set discConn=Nothing
,因此不必通过记录集或命令对象将其设置为Nothing,它是同一个连接对象
总而言之,您确实应该去掉代码中的以下几行:
Set.ActiveConnection=Nothing'断开连接代码>
discRS.Close()”据我所知,断开连接的记录集是指手动填充的记录集,而不是从数据库填充的记录集,例如用作多维数组或某种哈希表 所以,您所拥有的并不是一个断开连接的记录集,因为它是从数据库填充的,通过处理它的连接,您只会导致代码无法正常工作 由于代码中已有
,因此不必通过记录集或命令对象将其设置为Nothing,它是同一个连接对象 总而言之,您确实应该去掉代码中的以下几行:Set discConn=Nothing
Set.ActiveConnection=Nothing'断开连接代码>
discRS.Close()”在函数中,如果希望记录集返回到调用进程,则无法关闭并清理记录集 您可以清理任何连接和命令对象,但为了让您的记录集返回并重新填充,您只需不关闭或处置它即可 您的代码应该以如下方式结束:
'Cleanup discConn.Close() Set discConn = Nothing 'discRS.Close() 'Set discRs = Nothing 'Set discCmd = Nothing end function
在函数中,如果希望记录集返回到调用进程,则无法关闭和清理记录集 您可以清理任何连接和命令对象,但为了让您的记录集返回并重新填充,您只需不关闭或处置它即可 您的代码应该以如下方式结束:'Cleanup discConn.Close() Set discConn = Nothing 'discRS.Close() 'Set discRs = Nothing 'Set discCmd = Nothing end function
他确实在使用一个断开连接的记录集。我开始在VB6中使用它们。设置connection=Nothing,基本上有一个集合类,其中包含记录集的所有方便方法(即排序、查找、筛选等)。另外,您只需在获取记录所需的时间内保持连接,因此,当Microsoft通过该连接授权其服务器时,这是一种很好的方法,可以最大限度地减少每次连接的userm数量 记录集功能齐全,只是没有连接到数据源。您可以重新连接它,然后应用对其所做的任何更改
那是很久以前的事了,似乎功能已经被删除了 他确实在使用一个断开连接的记录集。我开始在VB6中使用它们。设置connection=Nothing,基本上有一个集合类,其中包含记录集的所有方便方法(即排序、查找、筛选等)。另外,您只需在获取记录所需的时间内保持连接,因此,当Microsoft通过该连接授权其服务器时,这是一种很好的方法,可以最大限度地减少每次连接的userm数量 记录集功能齐全,只是没有连接到数据源。您可以重新连接它,然后应用对其所做的任何更改
那是很久以前的事了,似乎功能已经被删除了 您应该使用
。然后可以断开记录集的连接。我创建了一个函数,将参数添加到命令字典对象,然后返回一个断开连接的记录集CursorLocation=adUseClient
Function CmdToGetDisconnectedRS(strSQL, dictParamTypes, dictParamValues) 'Declare our variables Dim objConn Dim objRS Dim Query, Command Dim ParamTypesDictionary, ParamValuesDictionary 'Open a connection Set objConn = Server.CreateObject("ADODB.Connection") Set objRS = Server.CreateObject("ADODB.Recordset") Set Command = Server.CreateObject("ADODB.Command") Set ParamTypesDictionary = Server.CreateObject("Scripting.Dictionary") Set ParamValuesDictionary = Server.CreateObject("Scripting.Dictionary") Set ParamTypesDictionary = dictParamTypes Set ParamValuesDictionary = dictParamValues Query = strSQL objConn.ConnectionString = strConn objConn.Open With Command .CommandText = Query .CommandType = adCmdText .CommandTimeout = 15 Dim okey For Each okey in ParamValuesDictionary.Keys .Parameters.Append .CreateParameter(CStr(okey), ParamTypesDictionary.Item(okey) ,adParamInput,50,ParamValuesDictionary.Item(okey)) Next .ActiveConnection = objConn End With objRS.CursorLocation = adUseClient objRS.Open Command , ,adOpenStatic, adLockBatchOptimistic 'Disconnect the Recordset Set objRS.ActiveConnection = Nothing 'Return the Recordset Set CmdToGetDisconnectedRS = objRS 'Clean up... objConn.Close Set objConn = Nothing Set objRS = Nothing Set ParamTypesDictionary =Nothing Set ParamValuesDictionary =Nothing Set Command = Nothing End Function
您应该使用
。然后可以断开记录集的连接。我创建了一个函数,将参数添加到命令字典对象,然后CursorLocation=adUseClient