List OLE DB providers installed on the system in C#

List OLE DB providers

This example shows how you can list OLE DB providers installed on the system. (I recently had to run an older program on a new system, and the provider used by the old program wasn’t installed. This example let me figure out what provider to use.)

When the program starts, the following code displays the installed providers in a DataGridView control.

private void Form1_Load(object sender, EventArgs e)
    OleDbEnumerator enumerator = new OleDbEnumerator();
    dgvProviders.DataSource = enumerator.GetElements();
    foreach (DataGridViewColumn col in dgvProviders.Columns)
        col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

The code creates an OleDbEnumerator object and uses its GetElements method to get a table holding a list of the OLE DB providers. It sets the DataGridView control’s DataSource property to that table. The code finishes by setting the AutoSizeMode property for each of the DataGridView control’s columns to make each column resize itself to fit its data.

After you know what OLE DB providers are available, you can use one in a database connection string. In my example, I used the following connection string.

// Compose the connection string.
string connect_string =
    "Provider=Microsoft.ACE.OLEDB.12.0;" +
    "Data Source=" + db_name + ";" +
    "Persist Security Info=False";

Here db_name was a variable holding the path to an Access database file.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in database, system and tagged , , , , , , , , , , . Bookmark the permalink.

2 Responses to List OLE DB providers installed on the system in C#

  1. Jose Luis says:

    Hi Mr. Stephens,
    Thanks a lot for your idea. It help me a lot!
    Thanks, thanks, thanks

  2. Tammar says:

    This is marvelous!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.