C# Retrieving correct DbConnection object by connection string?

DbConnection GetConnection(string connStr) { string providerName = null; var csb = new DbConnectionStringBuilder{ConnectionString=connStr}; if (csb. ContainsKey("provider")) { providerName = csb"provider".ToString(); } else { var css = ConfigurationManager . ConnectionStrings .Cast() .

FirstOrDefault(x=>x. ConnectionString==connStr); if (css! = null) providerName = css.

ProviderName; } if (providerName! = null) { var providerExists = DbProviderFactories . GetFactoryClasses() .Rows.Cast() .

Any(r=>r2. Equals(providerName)); if (providerExists) { var factory = DbProviderFactories. GetFactory(providerName); return factory.

CreateConnection(); } } return null; }.

If you're using framework 2.0 or above, and you can get them to pass in a second string with the driver class, you can use the dbProviderFactory class to load the driver for you. DbProviderFactory dbProviderFactory = DbProviderFactories. GetFactory(myDriverClass); DbConnection dbConnection = dbProviderFactory.

CreateConnection(); dbConnection. ConnectionString = myConnectionString; Here's an MSDN link to the Factory class: http://msdn.microsoft.com/en-us/library/wda6c36e.aspx.

You need to import the namespace System. Linq too, to use this code.

You should be able to parse out the Provider section and pass it into DbProviderFactories. GetFactory which will return a OdbcFactory, OleDbFactory or SqlClientFactory and let you then perform CreateConnection etc. I'm not sure how this would work with Oracle unless they provide an OracleDbFactory.

Most connection strings (at least in . NET 2.0) also have a providerName property that goes with them. So a SQL connection string will have a provider Name like: providerName="System.Data.

SqlClient" So your method would need to accept both the connection string and the provider name and then you could use the DbProviderFactory as mentioned by damieng.

Looks like a great solution, however, on using this code in my dll, I get 4 errors: Error 1 Instance argument: cannot convert from 'System.Configuration. ConnectionStringSettingsCollection' to 'System.Data. EnumerableRowCollection' D:\posts\App_Code\DAL\DataAccess.Cs 54 35 > D:\posts\ Error 2 'System.Configuration.

ConnectionStringSettingsCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data. EnumerableRowCollectionExtensions. Cast(System.Data.

EnumerableRowCollection)' has some invalid arguments D:\posts\App_Code\DAL\DataAccess. Cs 54 35 D:\posts\ Error 3 Instance argument: cannot convert from 'System.Data. DataRowCollection' to 'System.Data.

EnumerableRowCollection' D:\posts\App_Code\DAL\DataAccess. Cs 63 45 D:\posts\ Error 4 'System.Data. DataRowCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.

EnumerableRowCollectionExtensions. Cast(System.Data. EnumerableRowCollection)' has some invalid arguments D:\posts\App_Code\DAL\DataAccess.

Cs 63 45 D:\posts here's the libs that i'm referencing: using System; using System.Collections. Generic; using System. Data; using System.Data.Linq.

Mapping; using System.Data. Linq; using System. Configuration; using System.Data.

Common; using System. IO; using System. Web; using System.Web.

Caching; what am I missing, obviously, something is awry in my 'using' list. Hope you can help. Thanks - jimi.

Most connection strings (at least in . NET 2.0) also have a providerName property that goes with them. So your method would need to accept both the connection string and the provider name and then you could use the DbProviderFactory as mentioned by damieng.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions