【问题标题】:VBScript code error when using ActiveX to get data from the database使用 ActiveX 从数据库获取数据时出现 VBScript 代码错误
【发布时间】:2013-07-09 23:17:21
【问题描述】:

这是我的代码,我正在尝试打开与数据库的连接,然后使用 ActiveX 从表中的列中返回所有数据,然后将其输出到文本文档。我收到了这个错误。

PullData.vbs(41, 1) ADODB.Recordset:在 与请求的名称或序号相对应的集合。

这是我的代码,省略了敏感信息:

Const ForReading  = 1
Dim sServer
Dim sLogin
Dim sPwd
Dim sDb

Dim oCn 
Dim oRs 
sServer   = ""
sLogin    = ""
sPwd      = ""
sDb       = ""

Set oCn = CreateObject( "ADODB.Connection" ) ' set oCn to create an object called ADODB.Connection
Set oRs = CreateObject( "ADODB.Recordset"  ) ' set oRs to create an object called ADODB.Recordset

oCn.ConnectionString = "PROVIDER=SQLOLEDB" & _      
                       ";SERVER="   & sServer   & _
                       ";UID="      & sLogin  & _
                       ";PWD="      & sPwd    & _
                       ";DATABASE=" & sDb & " "
                       oCn.ConnectionTimeout=600
                       oCn.open 'Open the connection to the server

strSelString = "select CallID from dbo.CallLog" 'this is the SQL statement that runs a query on the DB

oRs.Open strSelString,oCn 'This opens the record set and has two parameters, strSelString and oCn

If oRs.EOF Then 'If open record set is at the end of file then...
  wscript.echo "There are no records to retrieve; Check that you have the correct record number." 'echo that there is no records to retrieve.
End if

'if there are records then loop through the fields
Do While Not oRs.EOF ' Do while not open record set is not end of file

strfield = ors("Tracker")

if strfield <> "" then 'If strfield doesn't equal "" then
  Set objFileSystem    = WScript.CreateObject("Scripting.FileSystemObject") 'Set objFileSystem to create object Scripting.FileSystemObject
  Set objOutPutFile    = objFileSystem.CreateTextFile("c:\test.txt", True) 'Set objOutPutFile to create object objFileSystem.CreateTextFile  
  strcomputer = oRs 'strcomputer is set to read the line
  wscript.echo strfield  
  objOutPutFile.WriteLine  strfield &"|" 
  objFileSystem.close
  objOutPutFile.close  
end if

oRs.MoveNext
oCn.Close
Loop

【问题讨论】:

    标签: vbscript activex adodb


    【解决方案1】:

    您要求CallID 列;

    "select CallID from dbo.CallLog"
    

    然后尝试阅读其他内容:

    strfield = ors("Tracker")
    

    所以要么选择Tracker,要么阅读CallID

    您也可以在循环之外创建/打开文件

    【讨论】:

    • 好的,我做了那个修改,我现在收到这个错误:(39, 1) ADODB.Recordset: Operation is not allowed when the object is closed.
    • oCn.Close 移出循环,在第一行之后关闭的位置...
    • 好的,一切正常,感谢您的帮助!
    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2020-09-02
    • 2019-11-12
    • 1970-01-01
    相关资源
    最近更新 更多