Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  ADO.NET  »  Create generic database code using ADO.NET 2.0 Provider Factories
Create generic database code using ADO.NET 2.0 Provider Factories
by David Jeavons | Published  10/17/2006 | ADO.NET | Rating:
David Jeavons
I have been programming database applications for almost 12 years starting out with VB3 and using every major version since then. I currently work for a retailer analysis company writing ASP.NET code for our ever evolving web site. I also enjoy helping other programmers with Visual Basic and .NET and am a moderator at vbCity. 

View all articles by David Jeavons...
The System.Data.Common Namespace

The new DBProviderFactory class lives in the System.Data.Common namespace. Within this Namespace you will see the usual suspects such as DbConnection, DbDataAdapter, DbCommand etc., but note how they are all prefixed with Db. Each of these classes are abstract classes and implement their respective interfaces in the System.Data Namespace. For example, the DbConnection class implements the IDbConnection interface, the DbDataAdapter implements the IDbDataAdapter interface and so on.

Another interesting point to note is that the concrete classes for each provider inherit their respective base classes in the System.Data.Common namespace. For example, the System.Data.OleDb.OleDbConnection class inherits the DbConnection class from the System.Data.Common namespace.  The same is true for the System.Data.SqlClient.SqlConnection class and all other provider classes that are installed on your machine.

The job of the DbProviderFactory class is to return concrete classes based on the provider you wish to target. However, each provider has its own implementation of the DbProviderFactory class, so you first need a method of retrieving the correct provider's implementation of the DbProviderFactory class. This is where the DbProviderFactories class comes in.

The DbProviderFactories class, like those above, lives in the System.Data.Common namespace and doesn't really do an awful lot. However, it is responsible for creating concrete DbProviderFactory classes based on the provider you are targeting. For example, if you wanted to target a Microsoft Access database then before you could use any of the concrete OleDb classes you first need an OleDbFactory. The DbProviderFactories class has a method called GetFactory which accepts either an invariant name (i.e. System.Data.OleDb) or a DataRow.

 Now, you may be wondering why a DataRow (I know I did), this is because the DbProviderFactories class has another method called GetFactoryClasses that will return a DataTable containing information regarding all of the providers that are installed on your machine. The GetFactory method knows what the structure of this DataTable is and is therefore able to take a DataRow containing the InvariantName and other information and return a concrete implementation of the provider's DbProviderFactory.

In the next section, we will explore the DbProviderFactories in a little more detail and see how we can retrieve a list of all installed providers and retrieve a concrete DbProviderFactory using the GetFactories and GetFactory methods respectively.

 

 

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 "@".
 
Comment #9  (Posted by Anke on 10/12/2008)
Rating
Words can't express your significance. ,
 
Comment #10  (Posted by Zena Madara on 10/12/2008)
Rating
the content has not been "broken down" by region. ,
 
Comment #11  (Posted by Maris on 11/25/2008)
Rating
The article is realy great and streigt forward.

.. but I have one problem i couldn't solve myself, so maybe someone can help?
The problem is with following line in code:
"Dim connectionString As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(connectionStringName)"

I have following error:"Type ConnectionStringSettings is not defined".
PS. I have added "Imports System.Configuration" at the begining of the form's code, but anyway it didn't work...
 
Comment #12  (Posted by an unknown user on 07/12/2009)
Rating
Wonderful article, everything covered in the article is explained well ... i would wish if a similar article on use of 'generic provider factories with data set and other disconnected data storage mechanisms' could be covered.
Also, if the codes can be provided in C# as well, then appeal of articles will increase.
 
Comment #13  (Posted by Buy oem Software on 02/10/2012)
Rating
T2y9Ex Right from this article begin to read this blog. Plus a subscriber:DD
 
Comment #14  (Posted by cheap oem software on 02/10/2012)
Rating
MG5zhy Fresh thoughts, fresh view on the subject..!!
 
Comment #15  (Posted by Discount OEM Software on 03/07/2012)
Rating
kg3Ze6 Wow, great blog article.Really thank you! Keep writing.
 
Comment #16  (Posted by Discount OEM Software on 03/07/2012)
Rating
PqN1wT Major thanks for the blog post.Really looking forward to read more. Want more.
 
Comment #17  (Posted by Adobe OEM Software on 03/07/2012)
Rating
vuQR8O Looking forward to reading more. Great blog.Thanks Again. Will read on...
 
Comment #18  (Posted by pranks on 03/08/2012)
Rating
I`m so grateful that you enlightened me and the most important thing that it happened in time. Just think, I have been using the internet for six years already but it`s the first time I`ve ever heard about it!...
 
Comment #19  (Posted by Microsoft OEM Software on 03/08/2012)
Rating
I3pafB Looking forward to reading more. Great blog article.Much thanks again. Awesome.
 
Comment #20  (Posted by web design sebastian florida on 03/09/2012)
Rating
Heartfelt thanks..!
 
Comment #21  (Posted by wholesale men clothing on 03/20/2012)
Rating
I appreciate you sharing this article post.Really thank you! Cool.
 
Comment #22  (Posted by wholesale men clothing on 03/20/2012)
Rating
Enjoyed every bit of your article post. Awesome.
 
Sponsored Links