7

我正在尝试从 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
4

3 回答 3

6

我的 VBScript 有点生疏,但你应该能够在 Recordset 上使用 .EOF 来检查它是否在结尾:

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

W3Schools 参考

于 2012-04-19T09:52:51.067 回答
1

我正在检查Recodset.EOFandRecordset.BOF以确保两者都是 False,但每次我收到提到的错误。这花了我几个小时,但我终于意识到,如果你调用andRecordset.Fields.count被改为.EOFBOFTrue

我希望这会很有用。

于 2017-08-03T17:15:21.863 回答
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
于 2012-04-19T23:38:55.980 回答