MySql.Data.MySqlClient.Replication.ReplicationManager throws an System.TypeInitializationException

会有一股神秘感。 提交于 2020-05-08 19:46:02

问题


I'm having trouble with my code to access a MySQL Database. Everytime I try to open my connection, a System.TypeInitializationException is thrown by MySql.Data.MySqlClient.Replication.ReplicationManager. Here is my code:

DataTable results = new DataTable("Results");
using (MySqlConnection connection = new MySqlConnection("SERVER=127.0.0.1;DATABASE=foo;UID=bar;PASSWORD=foobar;"))
{
    using (MySqlCommand command = new MySqlCommand(queryString, connection))
    {
        command.Connection.Open(); //throws System.TypeInitializationException
        command.ExecuteNonQuery();

        using (MySqlDataReader reader = command.ExecuteReader())
        results.Load(reader);
    }
}

Edit: I guess MySQL Driver was corrupt. After an upgrade from Windows 7 to Windows 10 everything worked fine.


回答1:


I had the same problem but when i installed the MySql.Data.dll using Nuget then the problem is solved Install MySql.Dll file from Nuget,

not from MySQL Website.




回答2:


Your connection string format is wrong. Try this :

static string cs = @"server=localhost;user id=bar;password=foobar;database=foo;";
DataTable results = new DataTable("Results");
using (MySqlConnection connection = new MySqlConnection(cs))
{
    using (MySqlCommand command = new MySqlCommand(queryString, connection))
    {
        command.Connection.Open(); //throws System.TypeInitializationException
        command.ExecuteNonQuery();

        using (MySqlDataReader reader = command.ExecuteReader())
        results.Load(reader);
    }
}


来源:https://stackoverflow.com/questions/41057256/mysql-data-mysqlclient-replication-replicationmanager-throws-an-system-typeiniti

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