Solved

Change ODBC Settings on the fly

Posted on 2003-12-12
7
2,133 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:randycarpet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 9927147
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
 
LVL 1

Author Comment

by:randycarpet
ID: 9927226
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
 
LVL 1

Author Comment

by:randycarpet
ID: 9927251
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 total points
ID: 9927396
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
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9927399
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
 
LVL 1

Author Comment

by:randycarpet
ID: 9928442
Excellent solution nmcdermaid.  Thank you very much!
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9928463
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question