I want to use some data from my company's database server in the administrative scripts that I'm writing. The database is a Microsoft SQL Server system. Can a script obtain data from a SQL Server database?
You can definitely use scripting to access the data in a SQL Server database. You can accomplish the task in several ways, including using VBScript, JScript, or Perl with the Win32:: ODBC extension. I describe how to use ADO from a VBScript script to access a SQL Server database, read the data from a table, and write that data to a file.
Before attempting to run this script, you need to be sure to have Microsoft Data Access Components (MDAC) installed on your system. MDAC provides the middleware support to connect your system to the SQL Server database. MDAC comes on Windows XP and Windows 2000 systems, but you'll need to install it on Windows NT and Windows 9x systems. You can download MDAC from http://www.microsoft.com/data.
The first line of code in the GetDBData.vbs script, which Listing 3 shows, turns on Option Explicit, which requires that all variables be declared. This requirement reduces your chance of making coding errors. The next few lines of code declare the variables that you'll use in the script. Next, in the Set the database connection information section of Listing 3, you set the database and logon information. The sServer, sLogin, and sPwd values need to match similar values on the SQL Server system that you're connecting to. In the Create the ADO Connection and Recordset objects section of Listing 3, the CreateObject function creates an ADO Connection object and an ADO Recordset object. The ADO Connection object connects to the database, and the Recordset object retrieves data from the database. You use the CreateObject function again in the next section of the listing to create a FileSystemObject named oFso, which the system will use to write data to the disk. . . .
ltunesi August 15, 2006 (Article Rating: