Database Example

VBScript does not provide native database access. As with other functions provided in the WSH environment, this is implemented using a COM object. ActiveX Data Objects (ADO) is a COM object that provides database access.

The following example uses the “Northwind” sample database that is shipped with Microsoft Access. This sample assumes that an ODBC entry called Northwind exists for the database.

Const adCmdText = 1
Dim objRst, objConn
Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Northwind"  
'execute the query against the provider
Set objRst = objConn.Execute("Select * From Customers",, adCmdText)

Do While Not objRst.EOF
    Wscript.Echo objRst("CompanyName")
    objRst.MoveNext
Loop
objRst.Close
objConn.Close

Get Windows XP in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.