C# ODBC Connection String for Access & Excel

Build connection strings and enumerate installed ODBC drivers (32bit and 64bit)

Before we can build the connection string we need to ensure that the ODBC drivers are available on our system.

 

ODBC Drivers

The standard ODBC drivers for MS Access 97 (*.mdb) and MS Excel 97 (*.xls) should be available on all Windows installations:

Microsoft Access Driver (*.mdb)
Microsoft Excel Driver (*.xls)

... but the MS Office 2010 ODBC drivers for *.xlsx and *.accdb might not be installed on your system.
This means it might be required to install the drivers first, if you want to use an Excel file with the extension .xlsx or an Access file with the extension .accdb.

Microsoft Access Driver (*.mdb, *.accdb)
Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)

 

 

Install ODBC Driver for *.accdb and *.xlsx

You can download the latest version of "Microsoft Access Database Engine (2010)" (for MS Access and MS Excel) here:
http://www.microsoft.com/en-us/download/details.aspx?id=13255

Important: There are different drivers for 32bit and 64bit applications. If you want to build a 32bit application, then you need to install the 32bit version of the driver, because a 32bit application cannot use the 64bit driver and a 64bit application cannot use the 32bit driver!

 

 

ODBC Connection String

The connection string for MS Access and MS Excel has the following syntax:

Driver={DRIVER};Dbq=FILEPATH;

We need only 2 parameter: the ODBC driver and the file path.
e.g.

Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\temp\somedatabase.mdb;
Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=C:\temp\somesheet.xlsx;

The "driver" can be different depending on the language, so it could be required to enumerate the installed drivers and search for the file extension.
e.g. "Microsoft Excel Driver" is called "Microsoft Excel Treiber" in German.

 

 

Enumerate ODBC Drivers

The system stores a list of the installed ODBC drivers in the registry HKEY_LOCAL_MACHINE.
There are two different locations for 32bit and 64bit drivers:

32bit: HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
64bit: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBCINST.INI\ODBC Drivers

Sample code to enumerate 32bit ODBC drivers:

using Microsoft.Win32;

using (RegistryKey reghklm = Registry.LocalMachine)
using (RegistryKey regdrivers = reghklm.OpenSubKey(@"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"))
{
    if (regdrivers != null)
    {
        foreach (string driver in regdrivers.GetValueNames())
        {
            // display drivers
            MessageBox.Show (driver);
        }
    }
}

 

 

Check if ODBC Driver is installed

We can build a small function to check if a specific ODBC driver (language) is available, e.g. "Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)"

using Microsoft.Win32;

private bool IsODBCDriverInstalled(string searchfor)
{
    using (RegistryKey reghklm = Registry.LocalMachine)
    using (RegistryKey regdrivers = reghklm.OpenSubKey(@"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"))
    {
        if (regdrivers != null)
        {
            foreach (string driver in regdrivers.GetValueNames())
            {
                if (driver.IndexOf(searchfor) != -1) return true;
            }
        }
    }
    return false;
}

...

// check if English(!) Excel (2010) driver is installed
string driver= "Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)";
if (IsODBCDriverInstalled(driver))
{
    // driver installed
}
else
{
    // driver not found
}

The function returns false if e.g. the German version is installed, so it might be better to enumerate the drivers and search for the file extension "*.xlsx".

 

 

Find Access or Excel ODBC Driver for File Extension

We just need to modify the code a bit, so that the function returns a valid driver (any language) for the given file extension.
The function returns an empty string if no valid driver was found.

using Microsoft.Win32;

private string GetODBCDriverForExtension(string extension)
{
    using (RegistryKey reghklm = Registry.LocalMachine)
    using (RegistryKey regdrivers = reghklm.OpenSubKey(@"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"))
    {
        if (regdrivers != null)
        {
            foreach (string driver in regdrivers.GetValueNames())
            {
                if (driver.IndexOf(extension) != -1) return driver;
            }
        }
    }
    return "";
}

...

// search for .xlsx (MS Excel 2010) driver
string xlsxdriver = GetODBCDriverForExtension("*.xlsx");
// search for .accdb (MS Access 2010) driver
string accdbdriver = GetODBCDriverForExtension("*.accdb");

If you prefer e.g. the English driver: check if the English driver is installed - otherwise use any valid driver for the file extension.

Disclaimer: The information on this page is provided "as is" without warranty of any kind. Further, Arclab Software OHG does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement