How to connect to mysql using mysql connector via C# without actually installing the connector

左心房为你撑大大i 提交于 2019-12-05 04:01:38

Try something like this (I didn't check any possible versions/configurations, but it works currently on my Vista x64 for MySql some 5.5... and .net connector 6.4.3.0 - using mysql.data.dll for v4 from .net/mono download).

Make sure the referenced mysql.data.dll assembly below is in your current directory.

using(var dt = new DataTable()) {
    dt.Columns.Add("Name");
    dt.Columns.Add("Description");
    dt.Columns.Add("InvariantName");
    dt.Columns.Add("AssemblyQualifiedName");
    dt.Rows.Add("Mysql something", 
        "mysql more", 
        "mysqlClient",
        "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d");

    var f = DbProviderFactories.GetFactory(dt.Rows[0]);
    using(var conn = f.CreateConnection()) {
        conn.ConnectionString = "your string here";
        conn.Open();
        // and do your work here.
        Console.WriteLine(conn);
    }
}

This page doesn't go into much detail about .NET Framework dependency, but I suspect that the Mysql connector might be dependent on a later version of the .NET framework.

In other words, your application uses Framework 2, but what version does the Mysql connector use?

This entry:

<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=5.0.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </DbProviderFactories>
</system.data>

... needs to be written into your machine.config.

I believe the default path should be something like:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

Update
You should be able to include MySQL .Net/Connector installer as part of your installation package. The entry in the machine.config is required for the same reasons that information is required in a web.config.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!