1

I use the MySQL 6.9.11.0 mysql.data dll,and the MySql Server 5.7.29

MySqlCommand cmd = new MySqlCommand();
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
      PrepareCommand(conn, null, cmd, cmdType, cmdText, cmdParms);
      MySqlDataAdapter da = new MySqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      conn.Close();
      cmd.Parameters.Clear();
      return ds;
}
private static void PrepareCommand(MySqlConnection conn, MySqlTransaction trans, MySqlCommand cmd, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)

{
    if (conn.State != ConnectionState.Open)
        conn.Open();
    cmd.Connection = conn;
    cmd.CommandText = cmdText;
    if (trans != null)
        cmd.Transaction = trans;
    cmd.CommandType = cmdType;
    if (cmdParms != null)
    {
        foreach (MySqlParameter parm in cmdParms)
            cmd.Parameters.Add(parm);
    }
}

and i got this error.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32 ByRef, Int64 ByRef)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32, Boolean)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlDataReader.Dispose(Boolean)
   at MySql.Data.MySqlClient.MySqlDataReader.Finalize()

Is there something wrong with this mysql.data dll? thanks in advance!

pioneer
  • 13
  • 3
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun Mar 29 '21 at 02:54
  • 1
    Hi @pioneer, can I know what is the `PrepareCommand` works for and also please attach the code for it. Thank you. – Yong Shun Mar 29 '21 at 03:01
  • I edit the question . Thank you – pioneer Mar 29 '21 at 05:51
  • I will recommend that to move out all the implementation in `ExecuteCommand` method and place in your Main function. For your current way, you initialize the connection, command in the `ExecuteCommand` method only, but it doesn't initialize in the Main function. – Yong Shun Mar 29 '21 at 05:58
  • Theoretically, this should not be a problem.but thank you – pioneer Mar 29 '21 at 10:21

1 Answers1

0

This is a known bug in MySql.Data: bug 91106.

You'll note that in the call stack, the exception is ultimately being thrown from MySql.Data.MySqlClient.MySqlDataReader.Finalize, which is the finalizer thread. This implies that you have a MySqlDataReader object that is not being disposed. Make sure you're disposing every MySqlDataReader object, preferably by using a using statement:

using (var reader = command.ExecuteReader())
{
    // use it
}

If you're not using MySqlDataReader directly, but just MySqlDataAdapter, you may want to consider switching MySQL drivers to avoid this bug. MySqlConnector (disclaimer: lead author) will be more robust because it doesn't use finalizers.

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • Unfortunately, we are using Net 4.0. How do I know if it is MySql.data 6.9.12 solved the problem? – pioneer Apr 01 '21 at 06:58
  • Try it and see. (But according to GitHub, the bug was fixed in 6.9.12: https://github.com/mysql/mysql-connector-net/commit/1e8e03536715f03625ff204f4940b37cd4bd81b6#diff-25e83b77b6678b622dd689468814bc42da3a95ad1065ead62b7e6d5db29c507b ) – Bradley Grainger Apr 01 '21 at 17:31
  • Thank you very much ! I'm trying to use 6.9.12. – pioneer Apr 02 '21 at 05:22