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 MySq
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.
In my case, it raised error only in production. It was due to a wrong assumption in the MySql.Data package. It for some reason seems to demand to be in the same folder as the orchestrating dll (it was referencing another dll which was referencing MySql). I resolved my issue by removing MySql.Data and instead installing MySqlConnector package as suggested in here.
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);
}
}