Rem: Scripting the Creation of Datasources

To automate the creation of datasources, you can use a script to create the required registry entries.

Michael Otey

August 15, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today


Q: How can I automate the creation ofODBC datasources that I use to accessMicrosoft SQL Server?

A: The HKEY_LOCAL_MACHINESOFTWAREODBCODBC.INI registrykey contains a subkey for each configuredODBC Data Source Name (DSN).The subkeys' entries store the DSNconfiguration settings. To create a datasource,you can simply create therequired registry subkey. I use a scriptcalled CreateDSN.vbs to create ODBCDSNs for SQL Server. You can downloadthis script from the WindowsScripting Solutions Web site. Go tohttp://www.winnetmag.com/windowsscripting, enter 43314 in the InstantDocID box, then click the 43314.zip hotlink.

CreateDSN.vbs begins by defininga set of variables that will contain theessential datasource elements, such asits name and the path to the appropriateODBC driver. As the CreateDSN.vbs excerpt in Listing 2 shows,the script then uses an instance of theFileSystemObject object to retrievethe path to the system folder, which iswhere ODBC drivers typically reside.Next, the script assigns values to thevariables that supply the DSN (whichthe sDB variable contains), the defaultdatabase's name (sDB), the DSN'sdescription (sDescription), the path tothe ODBC driver DLL (sDriverDLL),the SQL Server system's name(sSQLServer), and the driver's name(sDriverName). The sDriverDLL andsDriverName variables must use thevalues that you see in Listing 2. The values for the other variables can bewhatever you choose. Finally, thescript creates an instance of theWshShell object and uses that object'sRegWrite method to write the registrysubkey.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like