.Net MySql error “The given key was not present in the dictionary”

后端 未结 7 1356
深忆病人
深忆病人 2021-01-19 01:44

Trying to get simple count from table results in exception bellow. Tried different select statemens which also makes exception: \"SELECT * FROM goods\", but \"<

相关标签:
7条回答
  • 2021-01-19 02:25

    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

    0 讨论(0)
  • 2021-01-19 02:26

    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.

    0 讨论(0)
  • 2021-01-19 02:34

    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.

    0 讨论(0)
  • 2021-01-19 02:38

    Try using

    SELECT count(*) as count FROM goods
    
    0 讨论(0)
  • 2021-01-19 02:40

    Code is correct and all suggestion also should work. Just removed "collation-server" settings and restarted server and everything works as expected.

    0 讨论(0)
  • 2021-01-19 02:44

    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());
    
    0 讨论(0)
提交回复
热议问题