Click here to Skip to main content
Click here to Skip to main content
Go to top

Managing Data Sources in C#

, 10 May 2004
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Guy Baseke
Software Developer (Senior)
Canada Canada
Software engineer

Comments and Discussions

 
QuestionCreate DSN string using ODBCCP32.dll API PinmemberAlexeyD (UA)6-Sep-09 5:22 
QuestionCreate DNS without windows form PinmemberHenrique Magalhaes14-Jun-06 9:08 
Generalcannot close new odbc source window Pinmemberreklama.musor1-Dec-05 14:55 
GeneralRe: cannot close new odbc source window PinmemberJoeSimons14-Jul-06 15:00 
GeneralRe: cannot close new odbc source window PinmemberJoeSimons17-Jul-06 8:25 
GeneralMissing files PinmemberJon Lea9-Nov-05 7:30 
GeneralRe: Missing files PinmemberGuy Baseke10-Nov-05 12:57 
GeneralRe: Missing files PinmemberJon Lea10-Nov-05 13:00 
GeneralError PinsussTkachenko Andrey21-Jul-04 4:57 
GeneralError PinsussTkachenko Andrey21-Jul-04 4:57 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 11 May 2004
Article Copyright 2004 by Guy Baseke
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid