O'Reilly logo

ADO.NET Cookbook by Bill Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 + ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required