你的脚本有很多问题。我不打算逐一介绍,但请注意,记录集打开调用有更多参数。它们不是必需的,但对于光标类型的默认值是 adForwardOnly,对于锁定是 adLockReadOnly。使用默认值将无法从记录集中获取记录计数,因此我提供的代码仅显示记录集中第一条记录的值,以指示数据已被检索。
https://support.microsoft.com/en-us/help/272067/how-to-get-a-record-count-from-a-sql-server-ce-recordset
下面的代码有效,但您需要修改它以供您使用。运行脚本的批处理文件:
获取记录集.bat
cscript.exe /nologo GetRecordset.vbs
pause
要运行的脚本:
获取记录集.vbs
Dim objADORecordset
Dim strDBTableName
On Error Resume Next
'*****Modify below to a table name in your database*****
strDBTableName = "SomeDBTableName"
Set objADORecordset = CreateObject("ADODB.Recordset")
If Err.Number = 0 Then
GetRecordset "SELECT * FROM " & strDBTableName, objADORecordset
With objADORecordset
If Not .BOF Then
.MoveFirst
WScript.Echo "Field 0: " & .Fields(0).Value
Else
WScript.Echo "No records in recordset"
End If
End With
Else
WScript.Echo "Unable to create ADO Recordset"
End If
set objADORecordset = Nothing
WScript.Quit
'*********************************************
Sub GetRecordset(astrSQLQuery, aobjRecordset)
Dim strOLEConnect
Dim strUserID
Dim strUserPW
Dim objADOConnection
On Error Resume Next
WScript.Echo "SQLQuery: " & astrSQLQuery
strOLEConnect = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=CCMSProd;Data Source=sv-hfi-ccms;UID=vc;PWD=dw;"
strUserID = "vc"
strUserPW = "dw"
Set objADOConnection = CreateObject("ADODB.Connection")
If Err.Number = 0 Then
objADOConnection.Open strOLEConnect, strUserID, strUserPW
If Err.Number = 0 Then
aobjRecordset.Open astrSQLQuery, objADOConnection
If Err.Number <> 0 Then
WScript.Echo "Recordset open failed ERROR=" & Err.Number
End If
Else
WScript.Echo "Connection open failed ERROR=" & Err.Number
End If
Else
WScript.Echo "ADO connection failed ERROR=" & Err.Number
End If
Set objADOConnection = Nothing
End Sub
我将它与您的代码类似,以便您可以理解它,但它应该真正在 ADORecordset 之前创建并打开 ADO 连接。您通常会打开连接,运行所有必需的查询并在关闭连接之前处理它们。为对同一个数据库的不同查询多次创建到同一个数据库的新连接是没有意义的。它只会增加不必要的开销。我会把它留给你来改进你使用的代码。