1

We are supporting an old version of .Net framework, and we installed package MySql.Data.6.10.9 for the test.

The sample program was able to connect successfully to MySQL v8.0.32, but it gets "Error executing query: The given key was not present in the dictionary." when trying to run select version();.

See the test output below, and we are also attaching the sample code in the details section afterward.

.Net version: 4.0.30319.42000
Attempting to open connection...
Connection successful!
Error executing query: The given key was not present in the dictionary.
Press any key to continue . . .

We tried connection string of:

  • server=192.168.56.5;uid=root;port=3306;database=test-source;pwd=mysql_root_password, or
  • server=192.168.56.5;uid=root;port=3306;database=test-source;charset=utf8mb4;pwd=mysql_root_password

It gets the same error, so the following link did not resolve our issue. Or, please point out if we missed anything.

Details:

The full sample source code:

using System;
using MySql.Data.MySqlClient;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(".Net version: " + Environment.Version.ToString());
        string connectionString = "server=192.168.56.5;uid=root;port=3306;database=test-source;pwd=mysql_root_password";

        try
        {
            using (var connection = new MySqlConnection(connectionString))
            {
                Console.WriteLine("Attempting to open connection...");
                connection.Open();

                if (connection.State == System.Data.ConnectionState.Open)
                {
                    Console.WriteLine("Connection successful!");

                    // Retrieve MySQL version
                    try
                    {
                        string query = "SELECT VERSION();";
                        using (var command = new MySqlCommand(query, connection))
                        {
                            string version = command.ExecuteScalar().ToString();
                            Console.WriteLine("MySQL Version: " + version);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error executing query: " + ex.Message);
                    }

                }
                else
                {
                    Console.WriteLine("Failed to open the connection.");
                }
            }
        }
        catch (MySqlException ex)
        {
            Console.WriteLine("MySQL Exception: " + ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("General Exception: " + ex.Message);
        }
    }

}
3
  • 2
    This is not a mysql error, this comes from the driver. Many of the answers to the question you linked suggest to update mysql .net driver to resolve the issue. Commented Jan 25 at 0:29
  • 1
    MySql.Data.6.10.9 Is that the latest version? Commented Jan 25 at 4:23
  • Yes, we resolved the issue by upgrading the .Net Framework, SDK, then set the project to target the new .Net version, and update the MySql.Data driver. Commented Jan 27 at 19:13

1 Answer 1

0

We resolved the issue by upgrading the .Net and the driver:

  • Upgrade .Net Framework to v4.8.1,
  • .Net SDK to .Net 8.0 for its long-term-support,
  • changed the C# project property to target .Net Framework v4.8, and then
  • upgrade MySql.Data by calling Update-Package MySql.Data.

Then the issue got resolved, and we get the following test outputs:

CLR Version: 4.0.30319.42000
MySQL Driver Version: 9.2.0.0
Attempting to open connection...
Connection successful!
MySQL Version: 8.0.32
Press any key to continue . . .
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.