Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


July 11, 2002

Rem: Obtaining Data from a SQL Server Database


RSS
View this exclusive article with VIP access -- click here to join |
See More Windows 2000 Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

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. . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

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

ltunesi August 15, 2006 (Article Rating: )


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

bluenode March 29, 2007 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Articles Script Inventories Remote Computers and Writes the Information to a Database

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events SQL Server Unleashed EMEA

24 Hours of PASS

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement