Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

c# - SQL_ERROR: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception

I have an error with my C# code and MySQL. Can you help me solve it please ?

Here is my code :

try
{
     using (MySqlConnection dbcon = new MySqlConnection(SQLConnectionString))
     {
         dbcon.Open(); // ERROR HERE
         string sql = "SELECT * FROM scp_patreons";
         MySqlCommand command = new MySqlCommand();
         command = dbcon.CreateCommand();
         command.CommandText = sql;
         using (MySqlDataReader dr = command.ExecuteReader())
         {
             while (dr.Read())
             {
                 string fullname = (string)dr["fullname"];
                 plugin.Info("Name: " + fullname + "");
             }
         }
     }
 }
 catch (Exception ex)
 {
     plugin.Info("SQL_ERROR: " + ex.Message + "
" + ex.StackTrace);
 }


string SQLConnectionString = "Server=*; Database=*; User ID = *; Password=*; Pooling=false;SslMode=None;";

and i'm getting the error :

SQL_ERROR: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception.
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
  at MySql.Data.MySqlClient.NativeDriver.SetConnectAttrs () [0x00018] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate (System.Boolean reset) [0x00080] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.NativeDriver.Authenticate (System.String authMethod, System.Boolean reset) [0x0002a] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.NativeDriver.Open () [0x0033f] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.Driver.Open () [0x0000e] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x0004e] in <699dc28794d34867bd2008187eb8039c>:0 
  at MySql.Data.MySqlClient.MySqlConnection.Open () [0x0016d] in <699dc28794d34867bd2008187eb8039c>:0 
  at PlayerXP.PlayerXP.OnEnable () [0x00021] in <80e302c4eac64aeebd42276ff4a14839>:0 

I'm running 5.7.25 - MySQL Community Server (GPL)
Server : Localhost with UNIX socket Protocol version : 10
User : root@localhost
Server char : UTF-8 Unicode (utf8)
Mono JIT compiler version 5.18.0.268
Connector/NET 8.0.15
I use Debian 9.6.

Thanks for your help.

EDIT :

The new error message :

System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
at MySql.Data.MySqlClient.MySqlConnectAttrs..cctor () [0x0000f] in <699dc28794d34867bd2008187eb8039c>:0
--- End of inner exception stack trace ---
at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr)
at MySql.Data.MySqlClient.NativeDriver.SetConnectAttrs () [0x00018] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate (System.Boolean reset) [0x00080] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.NativeDriver.Authenticate (System.String authMethod, System.Boolean reset) [0x0002a] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.NativeDriver.Open () [0x0033f] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.Driver.Open () [0x0000e] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) [0x0004e] in <699dc28794d34867bd2008187eb8039c>:0
at MySql.Data.MySqlClient.MySqlConnection.Open () [0x0016d] in <699dc28794d34867bd2008187eb8039c>:0
at PlayerXP.PlayerXP.OnEnable () [0x00021] in <cc464d994e5a47c88df91f25374d2f29>:0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to @Bradley Grainger, my problem is solved. I have tried http://www.nuget.org/packages/MySqlConnector and it works !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...