+-
c#-惊人的错误“字典中不存在给定的键.”
我使用SqlCe作为数据库,但是当我将所有表复制到 MySql数据库中时,此异常开始了!

我的代码:

using (MySqlConnection conSQL = new MySqlConnection(conSQLSTR))
{
    try
    {
        conSQL.Open();
    }
    catch (MySqlException e)
    {
        string errMsg = e.Message;
        MessageBox.Show(errMsg, "error", MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
        return false;
    }
    using (MySqlCommand comSQL = conSQL.CreateCommand())
    {
        comSQL.CommandText = "SELECT " + Column + " FROM " + TableName
                                + " WHERE " + Column + "=@param";
        comSQL.Parameters.Add("@param", MySqlDbType.VarChar);
        comSQL.Parameters["@param"].Value = Value;
        MySqlDataReader drSQL;
        bool hasRows = false;
        try
        {
            drSQL = comSQL.ExecuteReader();
            hasRows = drSQL.Read();
        }
        catch (MySqlException e)
        {
            string errMsg =e.Message;
            MessageBox.Show(errMsg, "error", MessageBoxButtons.OK, 
                             MessageBoxIcon.Error);
        }
        if (!hasRows)
            return false;
        else
        {
            return true;
        };
    }
}

例外情况:

The given key was not present in the dictionary.

线:

drSQL = comSQL.ExecuteReader();

这很有趣,因为此异常未显示在消息框中!它就像一个运行时错误,似乎我的TRY根本没有用!

最佳答案
using (MySqlConnection conSQL = new MySqlConnection(conSQLSTR))

在后台,MySQL将使用字典将您的连接字符串解析为键/值对.确保您的连接字符串格式正确.

点击查看更多相关文章

转载注明原文:c#-惊人的错误“字典中不存在给定的键.” - 乐贴网