在DbProviderFactories.GetFactory期间,MySQL连接器失败

时间:2011-05-26 13:08:35

标签: mysql ado.net

我在使用dotnet框架4的VB.NET类库中使用Visual Studio 2010 SP1中的MySQL .Net Connector版本6.3.6。使用NuGet安装MySql.Data。这行代码失败了:

Dim qdb = Common.DbProviderFactories.GetFactory("MySqlClient1")

错误消息“无法找到或加载已注册的.Net Framework数据提供程序。”

如果我用“MySql.Data.MySqlClient”调用它:

Dim qdb = Common.DbProviderFactories.GetFactory("MySql.Data.MySqlClient")

错误变为“无法找到请求的.Net Framework数据提供程序。可能未安装。”

app.config 中我放了:

<?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>

如何获得MySql工厂?

1 个答案:

答案 0 :(得分:0)

要为MySql客户端使用DbProviderFactory,请在下面添加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>

代码用法

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;
}