Share via


How many records in a SQL table - vbscript example

Dim objConnection, objRecordset, strSearchCriteria
Const adOpenStatic = 3
Const adLockOptimistic = 3

' Connection string for SQL Server database.
strConnect = "DRIVER=SQL Server;" _
 & "Trusted_Connection=Yes;" _
 & "DATABASE=MyDatabaseName;" _
 & "SERVER=MyServerName\MyInstance"

' Connect to database.
Set adoConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

adoConnection.ConnectionString = strConnect
adoConnection.Open

objRecordset.Open "SELECT * FROM Table1", objConnection, adOpenStatic, adLockOptimistic

If objRecordset.EOF Then
 WScript.Echo "NO records could be be found."
Else
 WScript.Echo "Number of records: " & objRecordset.RecordCount
End If

odoConnection.Close
objRecordset.Close