"The given key 'utf8mb4' was not present in the dictionary

1

i have a problem with sql in C#.

I try to use ExecuteReader(), i get an error in this line:

MySqlDataReader reader = readcmdQuery.ExecuteReader();
System.Collections.Generic.KeyNotFoundException
  HResult=0x80131577
  Nachricht = The given key 'utf8mb4' was not present in the dictionary.
  Quelle = System.Private.CoreLib
  Stapelüberwachung:
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion version, String CharSetName)
   at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding()
   at MySql.Data.MySqlClient.MySqlField.set_CharacterSetIndex(Int32 value)
   at MySql.Data.MySqlClient.MySqlField.SetTypeAndFlags(MySqlDbType type, ColumnFlags flags)
   at MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
   at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[] columns)
   at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
   at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
   at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at decustomer___LAP_Projekt.MainWindow.MySQLConnector()

I thought it was a problem with Visual Studio or XAMPP.

I tried - add CHARSET=utf8 to the connection string - changing the "utf8mb4" to "utf8_german2_ci" in database - Execute SET NAMES 'utf8' - Updating MySQL - Tried on other client - Set up a new mysql server with MySQL Installer 8.0.20 (before: xampp)

The code snippets:

string connStr = CreateConnStr("localhost", "xxx", "root", "");
MySqlConnection connection = new MySqlConnection(connStr);
connection.Open();


public static string CreateConnStr(string server, string databaseName, string user, string pass)
        {

string connStr = "server=" + server + ";database=" + databaseName + ";uid=" +
user + ";password=" + pass + ";CHARSET=utf8;pooling=false;Allow User Variables=True";
return connStr;
        }
...
string readcmdQuery = "SELECT id, vorname, nachname FROM berater;CharSet=utf8;";
sqlcmd.CommandText = readcmdQuery;
MySqlDataReader reader = readcmdQuery.ExecuteReader();
consList.Items.Clear();
while(reader.Read())
...

Please help me. I don't found other solutions in the internet.

c#
mysql
mariadb
asked on Stack Overflow May 13, 2020 by Mario Wallner • edited May 13, 2020 by Mario Wallner

1 Answer

0

It seems like you might be using a very old version of MySql.Data; the problem you're reporting sounds like bug 58244, which was fixed a long time ago.

Or perhaps you've found a slightly different variant of that bug that manifests under slightly different conditions. If so, you could report it at https://bugs.mysql.com/ and wait for a fixed version.

However, I would recommend uninstalling MySql.Data and installing MySqlConnector instead. It's supported utf8mb4 from the beginning, adds async support, and fixes many other bugs.

answered on Stack Overflow May 13, 2020 by Bradley Grainger

User contributions licensed under CC BY-SA 3.0