查询SQL数据库时出现EOF和BOF错误

时间:2012-04-19 09:48:33

标签: sql vbscript

我正在尝试从vbs查询SQL数据库但是当没有找到记录时我收到错误

  

ADODB.Field:BOF或EOF为True,或者当前记录已被删除。请求的操作需要当前记录。

我想我需要使用IF NOT语句来捕获是否找不到记录,但我无法弄清楚它需要去哪里。

Do Until objFile.AtEndofStream
    strAppName = objFile.ReadLine

    ConnString="DRIVER={SQL Server};SERVER=aardvark002;UID=***;PWD=***;DATABASE=DEW_Users"
    SQL = "USE Win7AppData SELECT " & Chr(34) & strCountry & Chr(34) & " FROM AppsByCountry WHERE Application = '" & strAppName & "'"

    Set Connection = CreateObject("ADODB.Connection")
    Set Recordset = CreateObject("ADODB.Recordset")
    Connection.Open ConnString
    Recordset.Open SQL,Connection
    strApproval = Recordset(strCountry)
    If StrApproval = "YES" Then
        strApproval = "Approved"
    Else
        strApproval = "Denied"
    End If
    objExcel.Cells(intRow, 1).Value = strAppname 
    objExcel.Cells(intRow, 2).Value = strCountry
    objExcel.Cells(intRow, 3).Value = strApproval
    intRow = intRow + 1
Loop

3 个答案:

答案 0 :(得分:5)

我的VBScript上有点生锈,但您应该能够在Recordset上使用.EOF来检查它是否在最后:

Recordset.Open SQL,Connection
If Recordset.EOF = false Then
    ' have some rows, do what you want with them
End If

W3Schools reference

答案 1 :(得分:0)

如果结果有多条记录,你可能还有另一个错误,那么你需要移动你的记录指针,如果你不想以无限循环结束,我也会缩短你的代码,另一方面,如果你不打算再次使用它,你需要关闭你的连接。

ConnString="DRIVER={SQL  Server};SERVER=aardvark002;UID=***;PWD=***;DATABASE=DEW_Users"
Set Connection = CreateObject("ADODB.Connection")
Connection.Open ConnString
Do Until objFile.AtEndofStream
  strAppName = objFile.ReadLine
  SQL = "USE Win7AppData SELECT " & Chr(34) & strCountry & Chr(34) & " FROM AppsByCountry WHERE Application = '" & strAppName & "'"
  Set Recordset = Connection.Execute(SQL)
  Do While not Recordset.EOF 
    strApproval = Recordset(strCountry)
    If StrApproval = "YES" Then
      strApproval = "Approved"
    Else
      strApproval = "Denied"
    End If
    objExcel.Cells(intRow, 1).Value = strAppname 
    objExcel.Cells(intRow, 2).Value = strCountry
    objExcel.Cells(intRow, 3).Value = strApproval
    intRow = intRow + 1
    Recordset.MoveNext
  End If
Loop
Connection.Close
Set Connection = nothing

答案 2 :(得分:0)

我正在检查Recodset.EOFRecordset.BOF以确保两者都是False,但每次我收到上述错误。这花了我几个小时,但我终于意识到,如果你致电Recordset.Fields.countEOFBOF会更改为True

我希望这可能有用。