Click here to Skip to main content
Click here to Skip to main content

An ODBC (DSN/Driver) Manager DLL written in C# (Version - I)

By , 12 Jan 2004
 

Overview

Through this article I want to present readers with an ODBCManager class that allows user to have a list of ODBC Drivers installed in his/her system. Also this class gives the users all system as well as user DSNs.

Details

(Experienced readers can ignore/skim through this section. I have lifted most of the following stuff from the Internet)

About DSN: DSN is an acronym for Data Source Name. It's a simple and standard way to describe how to connect to a data source (database) using an ODBC driver. More importantly, using a DSN means we can change the location of our data by updating the DSN, no update to our application required. But we have to keep in mind that DSN's only describe ODBC connections, not OLEDB connections.

There are three types of DSN's; system, user, and file.

System DSN's and User DSN's differ only in who can access them on the system. A File DSN, however, is not really a datasource. It is a file that contains all the connection parameters used to connect directly to an ODBC driver. System and user DSN's are windows registry based, while File DSN's are stored in the file system. We normally create and administer DSN's through the ODBC Data Source Administrator, found in Control Panel on NT/9x machines, or in Administrative Tools for Win2K machines. However, there is nothing magical about editing DSN's. We can use regedit to view/modify user or system DSN's, and notepad works fine for file DSN's.

Most information about DSN's is stored in the registry.

  • HKEY_LOCAL_MACHINE\Software\ODBC\ODBCINST.INI contains a list of all ODBC drivers installed on the machine.
  • HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI contains all of system DSN's stored as separate subkeys.

ODBC.INI has two other subkeys, "ODBC Data Sources" that contains a list of all system DSN's, and "ODBC FileDSN", which contains the name of the folder where File DSN's are stored ("C:\Program Files\Common Files\ODBC\Data Sources" is the default). User DSN's are stored in HKEY_CURRENT_USER\Software\ODBC\ODBC.INI.

Choosing which type of DSN to use, depends on how our application works. In almost all cases a system DSN works. If we use a user DSN, then we'll have to do some extra work to make sure the DSN gets created when a user new to that machine logs in for the first time.

About ODBC Driver: An ODBC driver acts as a “translator” between an application and a database. There are drivers available for many databases like Oracle, MS Access, SQL Server, Foxbase etc. Main use of these drivers is, without having a client program supplied by the vendor of the data base, these drivers allow us to interact with databases. For example, let us suppose we have a Oracle Database which has some tables in it. Now If we have a Oracle ODBC Driver installed in our machine, then we don't need to have the Oracle S/W (Expensive) to talk with that database. But We have write our own routines to talk to the database using ODBC provided by C++/C# or whatever language we want to use (That support ODBC Driver API).

How this Class works

ODBCManager class is defined in 'ODBCMngr' namespace. This class is present in the DLL ODBCMngr.DLL. This class has all static methods. So no instance needs to be created (In fact users can't create one, since the class is following singleton pattern). Apart from the ODBCManager, the DLL has following other classes ODBCDriver and ODBCDSN.

This ODBCManager class has following static methods.

public static ODBCDriver[] GetODBCDrivers();
public static ODBCDriver GetODBCDriver(string driverName);
public static ODBCDSN[] GetSystemDSNList();
public static ODBCDSN GetSystemDSN(string dsnName);
public static ODBCDSN[] GetUserDSNList();
public static ODBCDSN GetUserDSN(string dsnName);
public static RegistryKey OpenComplexSubKey(RegistryKey baseKey, 
    string complexKeyStr, bool writable);

Except for the method 7 signatures of the above methods are self explanatory. I added method 7 to the class, since I don't find any method '.Net framework Registry class' that gives a RegistryKey for a nested key. For example to get the "Software" subfolder in HKEY_LOCAL_MACHINE root folder of registry following method call can be used.

RegistryKey fianlKey = (Registry.LocalMachine).OpenSubKey("Software");

Now if we want the key for "Software\ODBC\" in HKEY_LOCAL_MACHINE root folder of registry following method calls can be used.

RegistryKey fianlKey = (Registry.LocalMachine).OpenSubKey("Software");
fianlKey = fianlKey.OpenSubKey("ODBC");

Now my method (7) can eliminate multiple calls to OpenSubKey() method as done above. So using (7)

RegistryKey finalKey = ODBCManager.OpenComplexSubKey(
    Registry.LocalMachine, "Software\ODBC\", false);

Note: Last parameter is a boolean flag that tells the 'OpenComplexSubKey' method to create a sub folder if it is not present.

Program Notes

Since this is a DLL that has methods we can call, I don't really see the use of writing a client program. So I am not writing any program using the above DLL. Please note that this is version - I of the DLL. I will try to add more related stuff to this DLL in future. If I do so, I will update again.
I hope I explained all the things that are need to be explained. If any one of you feel I did some thing terribly wrong or not clear, please let me know. I will try to correct and/or respond.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Fact Pandit
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralInvitation to new programming Resources WebsitememberRavenet2 Aug '09 - 22:39 
GeneralSolution did not build in VS 2005memberGirdharr15 Jul '08 - 0:45 
GeneralRe: Solution did not build in VS 2005memberMark Mikulec4 Aug '08 - 8:26 
GeneralCreating/editing DSNsmembercronosxfiles_v25 Mar '07 - 0:13 
QuestionLicensememberburaksarica9 Nov '06 - 2:44 
GeneralGood JobmemberFayeeg30 Jun '06 - 10:17 
GeneralActual Path to a Databasememberiosens17 Mar '04 - 16:35 
GeneralRe: Actual Path to a DatabasememberFact Pandit9 Sep '04 - 9:06 
GeneralPath to system DSN Databasesmemberiosens17 Mar '04 - 16:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 13 Jan 2004
Article Copyright 2004 by Fact Pandit
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid