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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

nmcdermaidCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.