我有一组数据库服务器,并希望在循环中对所有数据库服务器执行相同的查询
但在第一次迭代后,我得到以下错误:
- 错误号码:3704
- 说明:Operation is not allowed when the object is closed
我实施的代码是:
Dim username Dim password Dim serverList(4) serverList(0) = "ServerAddress0" serverList(1) = "ServerAddress1" serverList(2) = "ServerAddress2" serverList(3) = "ServerAddress3" 'username and password are properly set for counter = 0 to UBound(serverList)-1 Set connObj = CreateObject("ADODB.Connection") Set rsObj = CreateObject("ADODB.Recordset") connString = .......... connObj.Open connString, username, password 'ERROR comes here, in second iteration. sqlScript = "SELECT * FROM ......" rsObj.open sqlScript, connObj Do While Not rsObj.EOF 'record set is fetched..... rsObj.MoveNext Loop 'current connection is closed rsObj.close connObj.close Next
注意:对于第一次迭代,此代码完美运行。第二次迭代出现错误。
答案 0 :(得分:1)
(0)禁用" On Error Resume Next" (如果你使用它)
(1)当UBound()返回最后一个索引而不是大小时,改变
for counter = 0 to UBound(serverList)-1
到
for counter = 0 to UBound(serverList)
(2)因为你可能弄乱了连接字符串,所以发布
的真实代码connString = ..........
(3)由于VBScript有Until
更改
Do While Not rsObj.EOF
到
Do Until rsObj.EOF
(4)因为您的记录集名为rsObj
更改
rsNice.MoveNext
到
rsObj.MoveNext
如果您确实正确构造了连接字符串,则(4)是错误的原因。
答案 1 :(得分:1)
我刚刚找到了解决这个问题的方法:
在迭代服务器时,我还试图关闭每个连接的连接对象。
......
rsObj.close
connObj.close
Next 'end of iteration over servers.
现在我将close语句移出循环。 即。在迭代时让对象打开。
......
Next 'end of iteration over servers.
rsObj.close
connObj.close
因此,它都与连接的打开/关闭状态有关。