10.13. Listing Installed OLE DB Providers

Problem

You need a list of the OLE DB providers installed on the machine running your code.

Solution

Use a SQL Server extended stored procedure or search the registry.

In the first case, the sample code executes the extended stored procedure xp_enum_oledb_providers. The result set containing the installed OLE DB providers is displayed.

In the second case, the sample code uses the Microsoft.Win32.Registry class to examine the registry, identify OLE DB provider subkeys, and retrieve and display the OLE DB provider names from these subkeys.

The C# code is shown in Example 10-13.

Example 10-13. File: OleDbProvidersForm.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Text;
using Microsoft.Win32;
using System.Data;
using System.Data.SqlClient;

//  . . . 

// SQL Server extended stored procedure StringBuilder result = new StringBuilder("Using SQL Server xp_enum_oledb_providers." + Environment.NewLine); int count = 0; SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings["Sql_Master_ConnectString"]); // Create a command to execute the extended stored procedure to // retrieve OLE DB providers. SqlCommand cmd = new SqlCommand("xp_enum_oledb_providers", conn); cmd.CommandType = CommandType.StoredProcedure; // Create the DataReader. conn.Open( ); SqlDataReader rdr = cmd.ExecuteReader( ); // Iterate through the OLE DB providers in the DataReader. while(rdr.Read( )) { result.Append(++count + ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.