Listing 1: Accessing a SQL Server Database ' Declare connection and connection string variables Dim conSqlServer, csSqlServer ' Declare command and command text variables Dim cmdContacts, ctContacts ' Declare recordset variable Dim rsContacts ' Declare argument constants Const adPersistXML = 1 Const adStateOpen = 1 ' Create Connection object Set conSqlServer = CreateObject("ADODB.Connection") ' Define connection string csSqlServer = "Provider='sqloledb';Data Source='ws04';" & _ "Integrated Security='SSPI';Initial Catalog='AdventureWorks';" ' Open connection conSqlServer.Open csSqlServer ' Create Command object and set the connection Set cmdContacts = CreateObject("ADODB.Command") Set cmdContacts.ActiveConnection = conSqlServer ' Define and set the command text ctContacts = "SELECT TOP 10 FirstName, LastName FROM Person.Contact" cmdContacts.CommandText = ctContacts ' Create and populate recordset object Set rsContacts = CreateObject("ADODB.Recordset") rsContacts.Open cmdContacts ' Save data to XML file rsContacts.Save "C:\Info\SqlServerContacts.xml", adPersistXML ' Clean up If rsContacts.State = adStateOpen then rsContacts.Close End If If conSqlServer.State = adStateOpen then conSqlServer.Close End If Set conSqlServer = Nothing Set csSqlServer = Nothing Set cmdContacts = Nothing Set ctContacts = Nothing Set rsContacts = Nothing