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
Solved

Change ODBC Settings on the fly

Posted on 2003-12-12
7
2,113 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

829 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