Change ODBC Settings on the fly

Hi

I have a VB app that is used as an interface for 2 databases.  The VB code uses only 1 system dsn so for users to switch between databases, they have to manually re-configure the odbc setting to look at the appropriate database.

In the long-term I will be changing the code but in the short term I would like to speed up the re-configuration process and am thinking a bat file would be fastest.

The question is:

Does anyone here know how to re-configure an odbc/dsn connection to look at another db by using a bat file, or, does anyone have a better short term solution.

Looking forward to your suggestions.


Cheers  -  Andy
LVL 1
randycarpetAsked:
Who is Participating?
 
nmcdermaidConnect With a Mentor Commented:
System DSN's are held in the registry under My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\ODBC

User DSN's are held in the registry under My Computer\HKEY_CURRENT_USER\SOFTWARE\ODBC

The registry can be automatically altered by double clicking a REG file (Possibly even running from the shell command) or you can code some big dirty API calls to do it automatically.

To create your REG files (this example will be for a machine DSN called myDSN)

1. Run Regedit
2. Go to the required branch... My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\myDSN
    Then press Registry/Export Registry file. This will create a .REG file for your DSN
3. Go to the 'header branch'.. My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources and do the same thing.. save it as a different file

4. In the header Reg file and cut the hive line and the line referrinf to your DSN these lines are like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources]
"myDSN"="Microsoft Access Driver (*.mdb)"

5. Paste this into your original REG... you now have a REG file which when double clicked, adds/modifies your DSN
6. Finally, make a copy of this file and just change the DB setting in it.... double click the required one to set the DSN settings



0
 
TimCotteeHead of Software ServicesCommented:
Hi randycarpet,

Whilst I would always advocate using DSN-less connections if you are using a DSN, a File DSN is the simplest to change, this is simply a file usually stored in \program files\common files\odbc\data sources which you can view/edit. It would be fairly trivial to write a batch file that creates a different file DSN for each database to be used this way. Or simply have both versions available as Database1.def and Database2.def that you simply copy over the default .dsn file using your batch process.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
randycarpetAuthor Commented:
Hi Tim,

I don't see how opening the file in "\program files\common files\odbc\data sources" helps me.  If I use notepad to open the file it just gives the name of the dsn (DSN=db1).

In my situation, the name of the dsn is always the same.  It's the db that the dsn points to that I want to change.  For example:

User wants to use vb interface for db1:
open odbc settings,
find dsn name
open dsn name
enter sql server login/pword info
change default database from db1 to db2

I hope I've explained that better.


Thanks  -  Andy
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
randycarpetAuthor Commented:
Damn.  My above comment might be a bit confusing due to vagueness and an error.  Here it is again:

VB App uses a dsn called MyDSN
MyDSN is used by the VB App to connect to 2 different databases: DB1 & DB2.  It's hard-coded in the VB App so I can't create 2 different dsn.

If the interface is set to look at DB1 and the user wants to look at data from DB2 the user has to:

~ Open odbc settings,
~ Open MyDSN
~ Enter sql server login/pword info
~ Change the default database from DB1 to DB2

I need a short-term solution to quicky change the default database of MyDSN, be that bat file or other suggestion.


Many thanks  -  Andy
0
 
nmcdermaidCommented:
PS note that you don't have to enter a password in the REG file... it depends on the driver but usually passwords are not saved to DSN's
0
 
randycarpetAuthor Commented:
Excellent solution nmcdermaid.  Thank you very much!
0
 
nmcdermaidCommented:
PS you could have used Tim Cottees suggestion... just have two file DSN's and copy them to the active DSN using a batch file. This solution might become necessary if the users don't have sufficent security to the registry.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.