It is currently Thu Oct 19, 2017 12:19 pm

Reply to topic  [ 1 post ] 
C# Enumerate ODBC DSNs 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15711
Location: On a slope
Reply with quote
Post C# Enumerate ODBC DSNs
So I'm coding an application to help monitor ODBC connections for my work. The idea is basically to connect in to ODBC connection and just write the results to a text file every X minutes.

However, finding a way to enumerate ODBC DSNs proved to be difficult. Turns out there's no API for it.. you have to manually tear the DSN information out of the registry. Here's the code I wrote... I have an arraylist I populate with instances of an 'ODBC' object which is just a custom object to hold the data. Nothing too complicated, but just for the hell of it I'll post the code for the object too.

     class ODBCDSN
        private string m_DSNName = null;
        private string m_DSNDriverName = null;
        private string m_DSNDriver = null;
        private string m_DSNServerName = null;

        public ODBCDSN(string dsnName, string dsnDriverName, string dsnDriver, string dsnServer)
            m_DSNName = dsnName;
            m_DSNDriverName = dsnDriverName;
            m_DSNDriver = dsnDriver;
            m_DSNServerName = dsnServer;

        public string getName()
            return m_DSNName;
        public string getDriverName()
            return m_DSNDriverName;
        public string getDriver()
            return m_DSNDriver;
        public string getServer()
            return m_DSNServerName;

And here's the code for the part that generates the ODBC DSN list.
        private ArrayList getDSNs()
            ArrayList DSNs = new ArrayList();
            //grab a list of the system DSN keys.  This will only get system DSNs, hence
            //why I'm opening HKEY_LOCAL_MACHINE\SOFTWARE\ODBC

            //the root ODBC Key
            RegistryKey baseKey = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ODBC");

            //The ODBC Data Sources key for a list of DSN names.  I use these later to enumerate
            //more specific info.
            RegistryKey dataSources = baseKey.OpenSubKey("ODBC.INI").OpenSubKey("ODBC Data Sources");
            string[] keyNames = dataSources.GetValueNames();

            //if there are no DSNs, just return the empty arraylist
            if (keyNames != null)
                //if there are keys, loop through them to get data.
                foreach (string keyName in keyNames)
                    //get the reg key for the specific DSN
                    RegistryKey dataSourceSpecifics = baseKey.OpenSubKey("ODBC.INI").OpenSubKey(keyName);
                        //grab some specific info about the DSN.  Note that some DSNs may not have this info...
                        //if they don't an exception will be thrown and they won't be added to the
                        //arraylist.  An example is Excel... this has no Server string.
                        string driverName = dataSources.GetValue(keyName).ToString();
                        string driver = dataSourceSpecifics.GetValue("Driver").ToString();
                        string server = dataSourceSpecifics.GetValue("Server").ToString();
                        ODBCDSN DSN = new ODBCDSN(keyName, driverName, driver, server);
                    catch { }
            return DSNs;

This code works though I'm not through coding the app yet. The code may end up changing depending on what I need and roadblocks I may run into. Also note that I'm only recording DSNs that contain Driver and Server strings/dwords. Not all DSNs do (such as Excel) so these wouldn't be added to the enumerated DSN list. This also only enumerates system DSNs... user DSNs are stored in a different part of the registry.

*edit* Fixed a typo in the class name. It's DSN, not DNS. :P

They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.

Mon Dec 08, 2008 10:25 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.