Solved

Change ODBC Settings on the fly

Posted on 2003-12-12
7
2,076 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
  • 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now