Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all,

I have a connectionstring and need to create the appropriate connection without knowing it to be a OleDBConnection, SQLConnection or ODBCConnection.
On the internet I read that I could use DbProviderFactories.GetFactoryClasses and find the right provider, based on the "Provider=" segment in the ConnectionString. Unfortunately this does not seem to work for me.

For example:
ConnectionString="Provider=SQLNCLI11.1;Integrated Security=false;Persist Security Info=False;User ID=XXX;Initial Catalog=DKOWare;Data Source=PC2014;Password=YYYY"

In this example the provider segment "Provider=SQLNCLI11.1" refers to SQL Server Native Client 11.1. This provider is installed on my computer.
When I create a SQLConnection based on the ConnectionString above, everything works fine.
There is no DbProviderFactory with the name "SQLNCLI11.1", so I'm not able to get an appropriate connection from DbProviderFactories.GetFactoryClasses.

Any ideas how I can resolve this?
Posted
Updated 18-Nov-15 1:18am
v2

"This provider is installed on my computer"
But is it registered in your machine.config or app.config?
When you run DbProviderFactories.GetFactoryClasses you'll get a datatable with a row for each installed provider.

To get the actual factory you need to run DbProviderFactories.GetFactory(ConnectionStringSetting.ProviderName)

Note, the column InvariantName in the factories datatable must match the providername in your connectionstring exactly.

You have some code you can look at here[^].
 
Share this answer
 
You're looking at the wrong "provider". The one in the connection string is the OLEDB provider. You need to look at the ProviderName on the ConnectionStringSettings object.

Connection Strings and Configuration Files[^]
XML
<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings>
        <add 
            name="Name" 
            providerName="System.Data.OleDb" 
            connectionString="Provider=SQLNCLI11.1;Integrated Security=false;Persist Security Info=False;User ID=XXX;Initial Catalog=DKOWare;Data Source=PC2014;Password=YYYY" 
        />
    </connectionStrings>
</configuration>

VB.NET
Dim settings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("Name")
Dim factory As DbProviderFactory = DbProviderFactories.GetFactory(settings.ProviderName)

Using connection As DbConnection = factory.CreateConnection()
    connection.ConnectionString = settings.ConnectionString
    ...
End Using

NB: If you're using the SqlClient classes, you can remove the Provider=SQLNCLI11.1; key from your connection string, since those classes don't use the OLEDB provider.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900