Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  ADO.NET  »  Create generic database code using ADO.NET 2.0 Provider Factories  »  DbProviderFactories
Create generic database code using ADO.NET 2.0 Provider Factories
by David Jeavons | Published  10/17/2006 | ADO.NET | Rating:
DbProviderFactories

The DbProviderFactories class, as mentioned previously, is responsible for returning concrete implementations of the DbProviderFactory class. In order to get a concrete DbProviderFactory, you first need to know the Invariant Provider Name that you will be targeting. This in itself is not difficult as the Invariant Names are simply System.Data.xxxx (where xxxx is OleDb, SqlClient, OracleClient, Odbc etc.), but instead of just relying on this piece of information, we can have a look at the GetFactoryClasses method which returns a DataTable containing information relating to all providers installed on your machine.

To demonstrate, create a new windows forms project and add a DataGridView to your form. Then use the following code to display the providers installed on your machine:

    1         'Retrieves a list of available providers and binds this list to the DataGrid.

    2         Dim availableProviders As DataTable = DbProviderFactories.GetFactoryClasses

    3         availableProvidersGrid.DataSource = availableProviders

Nb: This code assumes that you have imported the System.Data.Common namespace.

When you populate the grid, you will be presented with something similar to the following shortened table:

Name Description InvariantName AssemblyQualifiedName
Odbc Data Provider .Net Framework Data Provider for Odbc System.Data.Odbc ...
OleDb Data Provider .Net Framework Data Provider for OleDb System.Data.OleDb ...

The Name and Description fields are used to describe the provider in human readable terms. The InvariantName field however can be used to programmatically refer to the data provider you wish to target. The AssemblyQualifiedName field contains the fully qualified name of the factory class which is used to create a concrete implementation of the DbProviderFactory class you wish to target (omitted above due to space constraints).

Now that you know what providers are available on your system, the next task is to retrieve a concrete implementation of the required DbProviderFactory. The following example demonstrates how to retrieve the concrete implementation of the OleDbProviderFactory:

    1         Dim provider As DbProviderFactory = DbProviderFactories.GetFactory("System.Data.OleDb")

    2         MessageBox.Show(provider.GetType.ToString)

Assuming all went well, you should be presented with a message box stating "System.Data.OleDb.OleDbFactory". This is achieved by asking the DbProviderFactories to return a Factory (via the GetFactory method) based on the InvariantName string for System.Data.OleDb. If you change this InvariantName to System.Data.SqlClient, you should retrieve a message box stating "System.Data.SqlClient.SqlClientFactory". An important point to make at this time is that the InvariantName passed is case sensitive.

As mentioned previously, the GetFactory method also allows you to pass in a DataRow that contains this information via a call to the GetFactoryClasses method. If we combine the two examples above and pass in a DataRow to the GetFactory method you should get similar results. In the following example, I am passing row 2 of the availableProviders DataTable to return an instance of the OleDbFactory:

    1         Dim availableProviders As DataTable = DbProviderFactories.GetFactoryClasses

    2         Dim provider As DbProviderFactory = DbProviderFactories.GetFactory(availableProviders.Rows(1))

    3         MessageBox.Show(provider.GetType.ToString)

So, we now know how to retrieve the concrete provider factory that we want to target, the next step is in actually communicating with the provider (and hence the data store) without having to worry about which database we are actually using.

Note: The rest of this article assumes that you have both the Northwind databases for Microsoft Access and Microsoft SQL Server 2000/2005. If you are already running Microsoft SQL Server 2000 then you should already have this available to you. If you are running Microsoft SQL Server 2005 then you will need to download the Northwinds database which you can then attach to your instance of Microsoft SQL Server 2005.

Northwind and Pubs sample databases for Microsoft SQL Server 2000
Northwind Traders Sample Database (Microsoft Access 2000)

 

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 11/02/2006)
Rating
Really good article, are there any more articles related to generic databases
 
Comment #2  (Posted by David Jeavons on 11/02/2006)
Rating
Hi

Thanks for your comments. In terms of related articles, I didn't find much other than those on MSDN related to the DbProviderFactories that were useful. Each article I found discussed the theory but did not give any concrete examples.
 
Comment #3  (Posted by Jim on 11/17/2006)
Rating
Very nice. I wish the article had c# code as well.
 
Comment #4  (Posted by acostaf on 12/01/2006)
Rating
It´s really a good article for getting started with provider factories.
 
Comment #5  (Posted by an unknown user on 06/08/2007)
Rating
Very simple explanation for a really incredible addition to .Net 2.0. I'm using C# but the VB code is almost semantically identical (only differs syntactically). I was about to start writing my own "DbFactory" when I found this article, which is a story on itself: I've googled for "dbconnection providername" and found "Obtaining the DbProviderFactory" page on MSDN then I search for "using dbproviderfactories" and your article was the #2 listed. Good work!
 
Comment #6  (Posted by an unknown user on 03/16/2008)
Rating
Good article!
 
Comment #7  (Posted by an unknown user on 04/24/2008)
Rating
somthing iam not bleive it .... many thanks for you
 
Comment #8  (Posted by an unknown user on 05/23/2008)
Rating
The embedded SQL will only work for SQL server because of the parameter names, they start with "@".
 
Sponsored Links