Click here to Skip to main content
Licence CPOL
First Posted 10 May 2004
Views 65,407
Bookmarked 26 times

Managing Data Sources in C#

By | 10 May 2004 | Article
Data sources management.

Sample Image - Datasources.jpg

Introduction

Generally, developers write their own administration programs only if they want to retain complete control over data source configuration, or if they are configuring data sources directly from an application that is acting as an administration program. An administration program, the ODBC Administrator, is shipped with the Data Access SDK and can be redistributed by users of the SDK.

The problem

The need to let a user select his own data source is important in many applications. I set off to design a dialog that allows the user to select his own data source, and while I was doing so, I found out that my user will also need to create his own data source and sometimes change parameters for his data source (like tracing, login user, password, ...) thereby raising the need for the ODBC Administration program.

The Solution

ODBCCP32.dll contains functions to do data source administration. It is a Win32 DLL that exports API for managing data source. Being an unmanaged DLL, interop is a way to access the function. I wrote a class to wrap the functions I needed from the DLL and voila.

    public class ODBCCP32
    {
        public ODBCCP32()
        {
        }

        #region Interop Methods

        /// <SUMMARY>
        /// Win32 API Imports
        /// </SUMMARY>
        [DllImport( "ODBCCP32.dll")]private static 
             extern bool SQLManageDataSources(IntPtr hwnd);
        [DllImport( "ODBCCP32.dll")]private static 
             extern bool SQLCreateDataSource(IntPtr hwnd, string lpszDS);

        #endregion

        #region Methods
        public bool ManageDatasources(IntPtr hwnd)
        {
            return SQLManageDataSources(hwnd);
        }

        public bool CreateDatasource(IntPtr hwnd, string szDsn)
        {
            return SQLCreateDataSource(hwnd, szDsn);
        }
        #endregion
    }

To read data source information from the registry, two other classes were created; the first one to read registry, and the second one to read data source's information from the registry:

  1. How to read the list of data source names:
        public static string [] DsnList(HKEY hkey)
        {
            string []odbcs;
            if ( hkey == HKEY.CurrentUser )
                odbcs = HKCU.ValueNames(ODBC_SOURCES);
            else
                odbcs = HKLM.ValueNames(ODBC_SOURCES);
    
            return odbcs;
        }
  2. How to read the database name:
        public static string Database(string dsn, HKEY hkey)
        {
            string source;
            string database = "";
            string subkey = ODBCREG + "\\" + dsn;
    
            if ( hkey == HKEY.CurrentUser )
                source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn);
            else
                source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn);
    
            if ( source != "Microsoft Access Driver (*.mdb)" )
            {
                if ( hkey == HKEY.CurrentUser)
                  database = (string)HKCU.ReadOption(subkey, "database", database);
                else
                  database = (string)HKLM.ReadOption(subkey, "database", database);
            }
            else
            {
                if ( hkey == HKEY.CurrentUser)
                    database = (string)HKCU.ReadOption(subkey, "DBQ", database);
                else
                    database = (string)HKLM.ReadOption(subkey, "DBQ", database);
            }
    
            return database;
        }
  3. How to read the the server name:
        public static string Server(string dsn, HKEY hkey)
        {
            string source;
            string server = "";
            string subkey = ODBCREG + "\\" + dsn;
    
            if ( hkey == HKEY.CurrentUser)
                source = (string)HKCU.ReadOption(ODBC_SOURCES, dsn, dsn);
            else
                source = (string)HKLM.ReadOption(ODBC_SOURCES, dsn, dsn);
    
            if ( source != "Microsoft Access Driver (*.mdb)" )
            {
                if ( hkey == HKEY.CurrentUser)
                    server = (string)HKCU.ReadOption(subkey, "server", server);
                else
                    server = (string)HKLM.ReadOption(subkey, "server", server);
            }
    
            return server;
        }

Conclusion

For more information, see ODBC SDK Programmer's Reference, Chapter 23, Setup DLL Function Reference, and Chapter 24, Installer DLL Function Reference.

License

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

About the Author

Guy Baseke

Software Developer (Senior)

Canada Canada

Member

Software engineer

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCreate DSN string using ODBCCP32.dll API PinmemberAlexeyD (UA)5:22 6 Sep '09  
QuestionCreate DNS without windows form PinmemberHenrique Magalhaes9:08 14 Jun '06  
Generalcannot close new odbc source window Pinmemberreklama.musor14:55 1 Dec '05  
GeneralRe: cannot close new odbc source window PinmemberJoeSimons15:00 14 Jul '06  
GeneralRe: cannot close new odbc source window PinmemberJoeSimons8:25 17 Jul '06  
GeneralMissing files PinmemberJon Lea7:30 9 Nov '05  
GeneralRe: Missing files PinmemberGuy Baseke12:57 10 Nov '05  
GeneralRe: Missing files PinmemberJon Lea13:00 10 Nov '05  
GeneralError PinsussTkachenko Andrey4:57 21 Jul '04  
GeneralError PinsussTkachenko Andrey4:57 21 Jul '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 11 May 2004
Article Copyright 2004 by Guy Baseke
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid