MySQL connector fails during DbProviderFactories.GetFactory
I am using MySQL .Net Connector version 6.3.6 in Visual Studio 2010 SP1 in a VB.NET class library using dotnet framework 4. MySql.Data is installed using NuGet. This line of code fails:
Dim qdb = Common.DbProviderFactories.GetFactory("MySqlClient1")
With error message "Failed to find or load the registered .Net Framework Data Provider."
If I cal开发者_如何学编程l it with "MySql.Data.MySqlClient":
Dim qdb = Common.DbProviderFactories.GetFactory("MySql.Data.MySqlClient")
The error becomes "Unable to find the requested .Net Framework Data Provider. It may not be installed."
In app.config I put:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="MySqlClient1" />
<add name="MySQL Data Provider" invariant="MySqlClient1" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data.MySqlClient" />
</DbProviderFactories>
</system.data>
</configuration>
How can I get the MySql factory?
To use the DbProviderFactory for MySql client, add below config
<system.data>
<DbProviderFactories >
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Code usage
using (var conn = MySqlDBConnection.CreateConnection(connString))
{
// TODO
}
public static DbConnection CreateConnection(string connString)
{
DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
var conn = factory.CreateConnection();
conn.ConnectionString = connString;
return conn;
}
精彩评论