Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

Enumerating OleDb Providers

3.67/5 (5 votes)
22 Feb 2018CPOL 10.6K  
A simple way to enumerate the OleDb Providers installed on the system

Introduction

This tip presents a simple wrapper around the System.Data.OleDb.OleDbEnumerator.GetRootEnumerator method.

Background

I do a lot of database work with ADO.NET and I access many types of databases, some of which require OleDb, including Access and Excel. Listing the OleDb providers on a system is something I need to do from time to time, particularly when I get a new system. This small class makes accessing the enumeration just a little easier.

The Code

Your application needs only iterate the EnumerateProviders method.

C#
public sealed class OleDbProvider
{   
  /*\   
  |*| https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbenumerator.getrootenumerator
  |*| (v=vs.110).aspx
  |*| 0 SOURCES_NAME
  |*| 1 SOURCES_PARSENAME
  |*| 2 SOURCES_DESCRIPTION
  |*| 3 SOURCES_TYPE  One of the following enumeration members:
  |*|      Binder (0)
  |*|    , DataSource_MDP (1)
  |*|    , DataSource_TDP (2)
  |*|    , Enumerator (3).
  |*|      These correspond to the values returned in the SOURCES_TYPE column 
  |*|      of the native OLE DB sources rowset.
  |*| 4 SOURCES_ISPARENT
  |*| 5 SOURCES_CLSID
  \*/
          
  public enum ProviderType
  { Binder         = 0
  , DataSource_MDP = 1
  , DataSource_TDP = 2
  , Enumerator     = 3
  } ;

  public string       Name        { get ; private set ; }
  public System.Guid  ParseName   { get ; private set ; }
  public string       Description { get ; private set ; }
  public ProviderType Type        { get ; private set ; }
  public bool         IsParent    { get ; private set ; }
  public System.Guid  ClsID       { get ; private set ; }

  private OleDbProvider   
  (
    System.Data.IDataRecord Record
  )
  {         
    this.Name        =                     (string)       Record [ "SOURCES_NAME"        ] ;
    this.ParseName   = System.Guid.Parse ( (string)       Record [ "SOURCES_PARSENAME"   ] ) ;
    this.Type        =                     (ProviderType) Record [ "SOURCES_TYPE"        ] ;
    this.Description =                     (string)       Record [ "SOURCES_DESCRIPTION" ] ;
    this.IsParent    =                     (bool)         Record [ "SOURCES_ISPARENT"    ] ;
    this.ClsID       = System.Guid.Parse ( (string)       Record [ "SOURCES_CLSID"       ] ) ;
    
    return ;
  }
  
  public static System.Collections.Generic.IEnumerable<OleDbProvider>
  EnumerateProviders
  (
  )
  {
    using
    (
      System.Data.IDataReader dr
    =
      System.Data.OleDb.OleDbEnumerator.GetRootEnumerator()
    )
    {
      while ( dr.Read() )
      {
        yield return ( new OleDbProvider ( dr ) ) ;
      }
    }

    yield break ;
  }
}

History

  • 2018-02-22 First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)