What is the point of the DbProviderFactory Class

1,126 views
Skip to first unread message

Paul

unread,
Feb 27, 2007, 10:08:59 AM2/27/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hello All,

I am new to this so please bear with me. I have been exploring C# for
a few months, but I am only just getting into ASP.NET. Today I came
across the DBProviderFactory Class. I have been searching online for
an answer to my question without any luck. My question is this.

What is the point? What is the advantage of this code...

DbProviderFactory sqlClientFactory =
DbProviderFactories.GetFactory("System.Data.Client");
DbConnection cn1 = sqlClientFactory.CreateConnection();

when I can just use this code?

SqlConnection cn2 = new SqlConnection();

Is the first way necessay when developing ASP.NET websites as opposed
to developping Windows Forms applications?

Help is always appreciated. Thanks in advance.

Paul

Smith, Peter

unread,
Feb 27, 2007, 12:09:56 PM2/27/07
to DotNetDe...@googlegroups.com

 

 

 

On Tuesday, February 27, 2007 10:09 AM, Paul [heyth...@yahoo.ca] wondered:


> What is the point? What is the advantage of this code...

> 

> (DbProviderFactory code was here)

> 

>  when I can just use this code?

>

> SqlConnection cn2 = new SqlConnection();

 

The quick answer is: If you know you’re going to need an SqlConnection always and forever

in your code, there’s no advantage at all. But what if you want your code to be able to use either an

SqlConnection, or an OracleConnection? Then you want to use a DBProviderFactory to let the code determine

which connection to use, rather than hardcoding in some sort of switching logic.

 

 

Quoting from: http://msdn2.microsoft.com/en-us/library/wda6c36e.aspx

 

Working with Factories

 

The System.Data.Common namespace provides classes for creating DbProviderFactory instances to work with specific databases. When you create a DbProviderFactory instance, passing it information about the provider along with the connection string, the instance can determine the correct strongly-typed object to return based on the information it has been provided. This lets you write code that is not dependent on the data provider and lets you choose the provider at runtime.

 

All sorts of musing over the Factory Design Pattern at http://msdn2.microsoft.com/en-us/library/ms954600.aspx as well.

 

And a comparison article from the archives J is at http://msdn2.microsoft.com/en-us/library/ms379620(VS.80).aspx

 

NOTE: Before I read your request, a few minutes ago, I had no idea these DBFactory things existed. J Seemed like a reasonable question to look up, though. J

_____

Peter Smith

 

ekinwt

unread,
Feb 28, 2007, 1:07:07 AM2/28/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
if one day your program did not use SqlServer, but use Access, then
you must to change your code.
if code is "DbProviderFactory sqlClientFactory =

DbProviderFactories.GetFactory("System.Data.Client"); DbConnection cn1
= sqlClientFactory.CreateConnection(); ", You only need to change
"System.Data.SqlClient" to "System.Data.OleDb";
else if you code is "SqlConnection cn2 = new SqlConnection(); ", you
must Change the code everywhere you use it...

Paul

unread,
Mar 1, 2007, 11:55:58 PM3/1/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

Thanks for you help.

Paul

Reply all
Reply to author
Forward
0 new messages