c# - .Net MySql 错误 "The given key was not present in the dictionary"

标签 c# mysql .net

尝试从表中获取简单计数会导致以下异常。 尝试了不同的选择状态,这也有异常(exception):“SELECT * FROM goods”,但是“SELECT col1, col2 FROM goods” - 无一异常(exception)地工作。我究竟做错了什么?从工作台中,这些选择有效。

The given key was not present in the dictionary. System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at MySql.Data.MySqlClient.MySqlField.SetFieldEncoding() 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, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlDataReader.Close() at MySql.Data.MySqlClient.MySqlCommand.ResetReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySqlSybaseComparer.DbTester.Test(String& error) in c:\MySqlSybaseComparer\DbTester.cs:line 68

代码片段:

using (MySqlConnection conn = new MySqlConnection(ConStrMySql))
{
    try
    {
        conn.Open();
        using (MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM goods", conn))
        {
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                    MessageBox.Show(reader[0].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine + ex.ToString(););
    }
    conn.Close();
}

数据库的连接字符串:Server=localhost;数据库=艺术; uid=ramunas;密码=xxxx; AllowUserVariables=True;

最佳答案

2017 年

我已经解决了同样的错误,只需将字符集添加到连接字符串即可:

Server=myServer;Port=3306;Database=myDB15;User ID=usr33;Password=usr33P;CharSet=utf8;

在我的例子中,我使用的是 MySql Connector for .Net version 6.9.3。连接到 30 个具有相同结构、相同排序规则 (utf8_unicode_ci) 和不同表内容的相同数据库。

当我使用 MySqlCommand.ExecuteReader() 方法从 user 表中选择内容时,在某些数据库(30 个中的 4 个)中出现相同的错误 字典中不存在给定的键。

2022 年

几年后,我在使用 ObjectContext.ExecuteStoreQuery() 时遇到了同样的问题,但现在使用 MySql.DataMySql.Data.EntityFramework 在版本 8.0.27 上以及将数据库从 MySQL 5.7.38 迁移到 8.0.28 或 8.0.30 时。

向连接字符串添加 CharSetAllow User Variables 无效(并且无法更改我的 SELECT 或服务器设置)。

仅将 MySql.DataMySql.Data.EntityFramework 更新到版本 8.0.30 即可解决问题

关于c# - .Net MySql 错误 "The given key was not present in the dictionary",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41930040/

相关文章:

c# - 静态方法、抽象方法、接口(interface)方法比较?

php - 设置结果并在一个 if 中检查行数

c# - 是否可以从 XmlSerializer 序列化中排除某些类型的成员?

c# - ASP.NET Core MVC 模型属性绑定(bind)为 null

c# - 将服务器端纬度/经度值添加到 Google Map javascript

php - 更新存在超过 1 个最大值的数据库

mysql - 目前 Ubuntu 上的 puppetlabs-mysql 模块有什么技巧吗?

c# - 将集合 (HashSet) 隐式转换为 IEnumerable<T>

c# - LINQ to Entities 无法识别方法 'Int32 Int32(System.String)' 方法,并且无法将此方法翻译成存储表达式

c# - 找不到类型或命名空间名称 'Description'