Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#
Article

Managing Data Sources in C#

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
10 May 2004CPOL1 min read 92.8K   1.4K   26   10
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.

C#
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:
    C#
    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:
    C#
    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:
    C#
    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)


Written By
Software Developer (Senior)
Canada Canada
Software engineer

Comments and Discussions

 
QuestionCreate DSN string using ODBCCP32.dll API Pin
AlexeyD (UA)6-Sep-09 5:22
AlexeyD (UA)6-Sep-09 5:22 
QuestionCreate DNS without windows form Pin
Henrique Magalhaes14-Jun-06 9:08
Henrique Magalhaes14-Jun-06 9:08 
Generalcannot close new odbc source window Pin
reklama.musor1-Dec-05 14:55
reklama.musor1-Dec-05 14:55 
GeneralRe: cannot close new odbc source window Pin
JoeSimons14-Jul-06 15:00
JoeSimons14-Jul-06 15:00 
GeneralRe: cannot close new odbc source window Pin
JoeSimons17-Jul-06 8:25
JoeSimons17-Jul-06 8:25 
GeneralMissing files Pin
Jon Lea9-Nov-05 7:30
Jon Lea9-Nov-05 7:30 
GeneralRe: Missing files Pin
Guy Baseke10-Nov-05 12:57
Guy Baseke10-Nov-05 12:57 
GeneralRe: Missing files Pin
Jon Lea10-Nov-05 13:00
Jon Lea10-Nov-05 13:00 
GeneralError Pin
Tkachenko Andrey21-Jul-04 4:57
Tkachenko Andrey21-Jul-04 4:57 
GeneralError Pin
Tkachenko Andrey21-Jul-04 4:57
Tkachenko Andrey21-Jul-04 4:57 

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.