[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: List OLE DB providers 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.