Trying to get simple count from table results in exception bellow.
Tried different select statemens which also makes exception: \"SELECT * FROM goods
\", but \"<
After spending 5 hours researching how to fix it!!!.. i finally figured it out... all you need to do is to make sure that your 'MySql.Data.dll' is up to date! you can download it somewhere. or you can find it here C:\Program Files (x86)\MySQL\MySQL Installer for Windows\MySql.Data.dll.. :D
I have solved your same error simply adding the charset to the connection string:
Server=myServer;Port=3306;Database=myDB15;User ID=usr33;Password=usr33P;CharSet=utf8;
In my case I'm using MySql Connector for .Net version 6.9.3. to connect to 30 equal databases with the same structure, same collation (utf8_unicode_ci) and different table contents.
When I ran MySqlCommand.ExecuteReader() method to select content from user table, in some databases (4 of 30) a got the same error The given key was not present in the dictionary.
I had same problem using .net core 2.1, mysql, nhibernate.
After i added "Allow User Variables=True"
to connection string in appsettings.json my problem solved.
Try using
SELECT count(*) as count FROM goods
Code is correct and all suggestion also should work. Just removed "collation-server" settings and restarted server and everything works as expected.
In place of this statement:
using (MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM goods", conn))
Use:
using (var cmd = new MySqlCommand("SELECT COUNT(*) FROM goods", conn))
and then convert it to int value by using ExecuteScalar(). Something like this:
int count = Convert.ToInt32(cmd.ExecuteScalar());