【发布时间】:2014-05-14 17:36:11
【问题描述】:
所以,我被要求更新旧的 Classic ASP 网站。它没有使用参数化查询,并且输入验证很少。为了简化事情,我编写了一个辅助函数,它打开到数据库的连接,设置带有任何参数的命令对象,并创建一个断开连接的记录集 [我想!?! :)] 这是代码:
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
我的问题是,如果我在函数末尾调用discRS.Close(),则返回的记录集不会被填充。这让我想知道记录集是否确实断开了连接。如果我评论那条线一切正常。我还在设置ActiveConnection = Nothing 之前和之后使用discRS 值在函数中做了一些Response.Write(),它正确地返回了记录集值。所以它似乎被隔离到discRS.Close()。
我在4guysfromrolla.com 上找到了一篇旧文章,它在函数中发出了记录集Close()。我在其他网站上也看到过同样的事情。我不确定这是一个错误,还是有什么变化?
注意:我使用的是 Visual Studio Express 2013 中内置的 IIS Express
【问题讨论】:
-
您还可以使用 getRows() 并将记录集分配给内存中的变量。见:stackoverflow.com/questions/8916384/asp-getrows-count
-
Diodeus,可能,但是我必须记住什么元素在什么位置,而不是仅仅使用列名作为索引。
标签: vbscript asp-classic recordset