We are working with an older .NET Framework version and have installed the MySql.Data.6.10.9
package for testing.
The sample code successfully connects to MySQL v8.0.32, but when executing SELECT VERSION();
it returns the error: "Error executing query: The given key was not present in the dictionary."
Below is the test output and the connection strings we tried:
server=192.168.56.5;uid=root;port=3306;database=test-source;pwd=mysql_root_password
server=192.168.56.5;uid=root;port=3306;database=test-source;charset=utf8mb4;pwd=mysql_root_password
Both connection strings produce the same error. We also reviewed a related discussion on Stack Overflow, but it did not resolve our issue:
Below is 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);
}
}
}
What might be causing this error and how can it be resolved given our legacy .NET constraints?