Reputation: 85
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
, orserver=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);
}
}
}
Upvotes: 1
Views: 53
Reputation: 85
We resolved the issue by upgrading the .Net and the driver:
.Net Framework
to v4.8.1
,.Net SDK
to .Net 8.0
for its long-term-support,.Net Framework v4.8
, and thenMySql.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 . . .
Upvotes: 0