带有ADODB Recordset的VBS返回“无法找到带句柄-1的预准备语句”

时间:2018-03-23 21:01:08

标签: sql-server vbscript adodb

我写了一个VB脚本来浏览文件夹,从里面的文件名中提取记录号,查询相应区域的那些记录号,并根据需要重命名/移动文件。

首先我在里面创建一个文件记录集(rsFiles),然后从这些记录号中构建一个SQL字符串作为inlist,将相应的区域查询到第二个记录集(rsZones)。然后我浏览rsZones并将相应的区域写回rsFiles。

我的代码适用于少量文件(我已经测试并获得了高达200的可靠结果)但我需要一次处理几千个...如果我扔了太多文件,我会收到错误80040E14“无法找到带有句柄-1的预准备语句。”在rsZones.MoveFirst的那一行。

我认为这意味着rsZones没有任何记录,但是这不应该意味着rsZones.EOF会解析为TRUE吗?另外,我将ADODB命令超时设置为0,那么为什么我没有在更大的入侵列表中获取记录?

下面的代码摘录。任何帮助/想法将不胜感激。谢谢!!

Const sSourceDir = "C:\MyDir\"

Set re = New RegExp

re.Global = False

Set rsFiles = CreateObject("ADOR.Recordset")

rsFiles.Fields.Append "File", adVarChar, MaxCharacters
rsFiles.Fields.Append "Record_Number", adVarChar, 10
rsFiles.Fields.Append "Zone", adVarChar, 2
rsFiles.Open

re.Pattern = "(\d{1,})_\d{1,}_[IPG]\S*.pdf"

Set oFolder = oFSO.GetFolder(sSourceDir)

Set oFiles = oFolder.Files

If oFiles.Count > 0 Then 

    For each oFile in oFiles

        Set reMatches = re.Execute(oFile.Name)

        If reMatches.Count > 0 Then 

            sRecordNumber = reMatches(0).subMatches(0)

            sInlist = sInlist & "'" & sRecordNumber & "',"

        Else

            sRecordNumber = ""

        End If 

        rsFiles.AddNew
        rsFiles("File") = oFile.Name
        rsFiles("Record_Number") = sRecordNumber

    Next

End If 

If sInlist = "" Then 

    MsgBox "No files matching record number pattern"

    wScript.Quit

End If 

sInlist = Left(sInlist, Len(sInlist)-1)

Const sConn = "Provider=SQLOLEDB;SERVER=my\server;DATABASE=default_database;TRUSTED_CONNECTION=yes"

sSQL = "select tablea.record_number, tablea.zone from tablea where tablea.record_number in (" & sInlist & ")"

Set oConn = CreateObject("ADODB.Connection") 
oConn.ConnectionTimeout = 0
oConn.Open sConn 

Set cmd = CreateObject("ADODB.Command") 

With cmd
    .ActiveConnection = oConn 
    .CommandText = sSQL 
    .CommandType = 1 'adCmdText 
    .CommandTimeout = 0
    .Prepared = True 
End With

set rsZones = CreateObject("ADODB.Recordset") 

Set rsZones = cmd.Execute 

If Not rsZones.EOF Then 

    rsZones.MoveFirst 'ERROR IS HERE

    Do until rsZones.EOF

        rsFiles.Filter = 0
        rsFiles.Filter = "record_number = '" & rsZones(0) & "'"

        rsFiles.MoveFirst

        Do Until rsFiles.EOF

            rsFiles("Zone") = rsZones("Zone")

            rsFiles.MoveNext

        Loop

        rsZones.MoveNext

    Loop

End If 

rsZones.Close 

0 个答案:

没有答案