Create a System DSN
You can programmatically create a System DSN (Data Source
Name) very easily. Typically, DSNs are created through the ODBC Data
Source Administrator window in Control Panel. This page shows how to create one
on the fly for an MS Access database.
Creating a DSN is simply a matter of writing a few registry values. The hard
part is knowing what to write. The best way to figure this out is to create a
DSN through the ODBC Data Source Administrator then look in the registry to see
the keys and values required.
Download Source Code
ODBC entries are located in the registry in two spots. Both are under the
HKEY_Local_Machine\Software\ODBC key. The first is found in the
ODBC Data Sources subkey.
The ODBC Data Sources subkey contains a single string value. The name of this
value is simply the name you want to assign to the DSN. This is the name
displayed in the ODBC Data Administrator window. The value is the name of the
associated driver, "Microsoft Access Driver (*.mdb)" in this case.
The second set of registry entries go in the ODBC.INI
subkey. Under this key you must create a subkey with the same name used in the
previous step. In my sample I called it "MS Access DSN". This is where you
write values for the path to the database and driver, the description used for
the DSN and the security info such as user ID and password.
With MS Access, you need to create a few keys underneath this key as shown in
the program. In general, the keys and values required will differ with the
database you are trying to connect to.
If you want your new DSN to appear in the ODBC Data Source Administrator window
you must make one final entry. This entry goes in the ODBC.INI
file found in your Windows folder.
First you must create a new section. The section name is the same as that of
your DSN, example: "[MS Access DSN]". Finally you create a "Driver32" item
whose value is the path to the database driver. That's it.
Download the project. Run it and change the default values as necessary. If you
are using an Access database you should not have to change too much. If you
need to create a DSN for SQL Server, Visual FoxPro, Excel or something other
than Access you will need to modify the program.
After you create the DSN you can see it by looking in the registry. If you chose
to "display it" by checking the check box then you can see it in the ODBC Data
Source Administrator.
|