SQL 查询SQL数据库时出现EOF和BOF错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10225551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EOF and BOF error when querying SQL database
提问by chemist
I'm trying to query a SQL database from vbs but when no record is found I get an error
我正在尝试从 vbs 查询 SQL 数据库,但是当没有找到记录时,我收到一个错误
ADODB.Field : Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
ADODB.Field : BOF 或 EOF 为 True,或当前记录已被删除。请求的操作需要当前记录。
I think I need to use an IF NOT statement to capture if the record isn't found but I can't figure out where it needs to go.
我想我需要使用 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
采纳答案by AdaTheDev
Bit rusty on my VBScript, but you should be able to use .EOF on the Recordset to check if it's at the end:
我的 VBScript 有点生疏,但你应该能够在 Recordset 上使用 .EOF 来检查它是否在最后:
Recordset.Open SQL,Connection
If Recordset.EOF = false Then
' have some rows, do what you want with them
End If
回答by Bruno Silva
I was checking the Recodset.EOF
and Recordset.BOF
to make sure that both are False, but everytime I was receiving the mentioned error. That took me some hours but I finally realized that if you call the Recordset.Fields.count
the EOF
and BOF
are changed to True
.
我正在检查Recodset.EOF
和Recordset.BOF
以确保两者都是错误的,但是每次我收到提到的错误时。这花了我几个小时,但我终于意识到,如果你调用Recordset.Fields.count
theEOF
并BOF
改为True
.
I hope this can be useful.
我希望这会很有用。
回答by peter
You could have another bug also in case you have more than one record as result, then you need to move your recordpointer if you don't want to end in an endless loop, i also shortened your code a bit, on the other hand you need to close your connection if you'r not going to use it again.
如果结果有多个记录,您也可能有另一个错误,如果您不想以无限循环结束,则需要移动记录指针,我还稍微缩短了您的代码,另一方面,您如果您不打算再次使用它,则需要关闭您的连接。
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