Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

July 11, 2002 12:00 AM

Rem: Obtaining Data from a SQL Server Database

Windows IT Pro
InstantDoc ID #25628
Rating: (2)
Downloads
25628.zip

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.

After you create all the required objects, you need to set the ConnectionString property of the ADO Connection object to an OLE DB connection string that contains the login information necessary for connection to the database. Then, you need to use the oRs.Open line to open the oRs Recordset object. The first parameter is a SQL Select statement that retrieves two columns from the authors table in the SQL Server pubs database that this example uses. The second parameter links the Recordset object to an active Connection object.

After the Recordset is filled, the FileSystemObject's CreateTextFile method creates a new text file named datafile.txt. Then, within the While loop, a MoveNext method reads all the rows in the ADO Recordset object. As each row is read, a For...Next loop parses each data row. Within the For...Next loop, the script extracts the data for each column by accessing the Value property of each ADO Field object. The script uses that data to build a string in which commas separate each data value. After the script extracts all column data and builds the string, the FileSystemObject's WriteLine method writes the data to the datafile.txt file.

After all rows have been read and the data has been written to a disk, the script closes the oFso FileSystemObject, the oRs Recordset object, and the oCn Connection object. You can use the resulting database information in subsequent scripts or processes.

Related Content:

ARTICLE TOOLS

Comments
  • EDWARD
    5 years ago
    Mar 29, 2007

    If I am logged it WHY CAN I NOT SEE THE ARTICAL!

  • LORENZO
    6 years ago
    Aug 15, 2006

    This was exactly what I was looking for. Thank you so much for posting this very useful generic script.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.