Click here to Skip to main content
13,295,178 members (73,108 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 14 Jan 2004

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

, 14 Jan 2004
Rate this:
Please Sign up or sign in to vote.
ODBC Manager class that retrieves System and User DSNs and driver list.


Programmers who are using ODBC to connect to any database, and wish to program in C#/.NET.


Through this article, I want to present readers with an ODBCManager class that allows a 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.

Hard Words

(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 is required. But we have to keep in mind that DSNs only describe ODBC connections, not OLEDB connections.

There are three types of DSNs; system, user and file.

System DSNs and User DSNs differ only in who can access them on the system. A file DSN, however, is not really a data source. It is a file that contains all the connection parameters used to connect directly to an ODBC driver.

System and user DSNs are Windows registry based, while file DSNs are stored in the file system. We normally create and administer DSNs 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 DSNs. We can use regedit to view/modify user or system DSNs, and Notepad works fine for file DSNs.

Most information about DSNs 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 the system DSNs stored as separate subkeys.

ODBC.INI has two other subkeys, ODBC Data Sources that contains a list of all system DSNs, and ODBC FileDSN, which contains the name of the folder where file DSNs are stored (C:\Program Files\Common Files\ODBC\Data Sources is the default). User DSNs 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 an Oracle database which has some tables in it. Now, if we have an 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 to write our own routines to talk to the database using ODBC provided by C++/C# or whatever language we want to use (That supports 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 (Infact users can't create one, since the class is following singleton pattern). Apart from the ODBCManager, the DLL has two other classes: ODBCDriver and ODBCDSN.

This ODBCManager class has the following static methods.

  1. public static ODBCDriver[] GetODBCDrivers();
  2. public static ODBCDriver GetODBCDriver(string driverName);
  3. public static ODBCDSN[] GetSystemDSNList();
  4. public static ODBCDSN GetSystemDSN(string dsnName);
  5. public static ODBCDSN[] GetUserDSNList();
  6. public static ODBCDSN GetUserDSN(string dsnName);
  7. public static RegistryKey OpenComplexSubKey(RegistryKey baseKey, string complexKeyStr, bool writable);

Except for method 7, the signatures of the above methods are self explanatory.

I added method 7 to the class, since I don't find any method in the .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 the registry, the 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 the registry, the 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 = 
  "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 needed 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fact Pandit
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralAdd new DSN/Driver entry Pin
ZejulioZ18-Dec-08 19:51
memberZejulioZ18-Dec-08 19:51 
GeneralRe: Add new DSN/Driver entry Pin
rzubi26-Mar-09 1:05
memberrzubi26-Mar-09 1:05 
GeneralRe: Add new DSN/Driver entry Pin
ZejulioZ26-Mar-09 3:58
memberZejulioZ26-Mar-09 3:58 
GeneralRe: Add new DSN/Driver entry Pin
Goobang26-Mar-09 10:36
memberGoobang26-Mar-09 10:36 
GeneralRe: Add new DSN/Driver entry Pin
ZejulioZ26-Mar-09 15:22
memberZejulioZ26-Mar-09 15:22 
GeneralRe: Add new DSN/Driver entry Pin
Edy Chandra22-Jun-09 23:39
memberEdy Chandra22-Jun-09 23:39 
GeneralRe: Add new DSN/Driver entry Pin
ZejulioZ23-Jun-09 15:00
memberZejulioZ23-Jun-09 15:00 
GeneralRe: Add new DSN/Driver entry Pin
Edy Chandra25-Jun-09 21:45
memberEdy Chandra25-Jun-09 21:45 
GeneralThank you very much. Pin
Muaddubby3-Apr-08 12:10
memberMuaddubby3-Apr-08 12:10 
QuestionADO.NET 2.0 Pin
erhm14-Mar-08 9:19
membererhm14-Mar-08 9:19 
QuestionCall from webservice Pin
GuyV19-Nov-07 4:46
memberGuyV19-Nov-07 4:46 
AnswerRe: Call from webservice Pin
richardbrigzy4-Nov-09 0:10
memberrichardbrigzy4-Nov-09 0:10 
GeneralCredit Pin
guNaSupport23-Oct-07 22:38
memberguNaSupport23-Oct-07 22:38 
Generalthanks Pin
Guilherme Labigalini2-May-07 2:05
memberGuilherme Labigalini2-May-07 2:05 
GeneralConcerning OpenComplexSubKey Function Pin
Mark Toma15-Nov-06 19:45
memberMark Toma15-Nov-06 19:45 
Questionusing dsn to connect to DB Pin
venkateshmvp16-Oct-06 1:47
membervenkateshmvp16-Oct-06 1:47 
GeneralSample Code Needed Pin
atomicfroman15-May-06 3:21
memberatomicfroman15-May-06 3:21 
GeneralRe: Sample Code Needed Pin
Fact Pandit15-May-06 9:02
memberFact Pandit15-May-06 9:02 
GeneralRe: Sample Code Needed Pin
atomicfroman15-May-06 9:10
memberatomicfroman15-May-06 9:10 
GeneralSample Code Pin
binukoshy18-Apr-06 3:16
memberbinukoshy18-Apr-06 3:16 
GeneralRe: Sample Code Pin
Fact Pandit15-May-06 9:03
memberFact Pandit15-May-06 9:03 
GeneralRe: Sample Code Pin
Amit Sharam11-Sep-08 23:21
memberAmit Sharam11-Sep-08 23:21 
GeneralLicence Pin
mark_ledwich5-Apr-06 19:20
membermark_ledwich5-Apr-06 19:20 
GeneralRe: Licence Pin
Fact Pandit15-May-06 9:06
memberFact Pandit15-May-06 9:06 
GeneralRe: Licence Pin
GuardianStorm12-Jul-06 4:16
memberGuardianStorm12-Jul-06 4:16 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 15 Jan 2004
Article Copyright 2004 by Fact Pandit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid