【问题标题】:Why it doesn't work when I use MysqlConnection.open()? [closed]为什么我使用 MysqlConnection.open() 时它不起作用? [关闭]
【发布时间】:2014-09-19 12:04:20
【问题描述】:
  using System;
    using System.Collections.Generic;
    using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;


namespace dbTest
{
    class Program
    {
        static void Main(string[] args)
        {

        string sqlStr   = "Database=weather;Server=127.0.0.1;Uid=root;Password=123456;pooling=false;CharSet=UTF8;port=3306";

          MySqlConnection mysql = new MySqlConnection(sqlStr);

             mysql.Open();

             Console.WriteLine("SUCCESS");

              mysql.Close();
        }
    }
}

运行语句“mysql.Open()”会崩溃

有人知道为什么吗? 错误信息是: -未处理的异常:System.Collecions.Generic.KeyNotFoundException:密钥不在字典中 -在 System.Collections.Generic.Dictionary'2.get_Item(TKEY KEY) -在 Mysql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion 版本,字符串 CHARSETNAME) -在 Mysql.Data.MySqlClient.CharSetMap.GetEncoding(DVversion 版本,字符串 CharSetName) - 在 Mysql.Data.MySqlClient.Driver.Configure(MySqlConnection 连接) -在 Mysql.Data.MySqlClient.Mysqlconnection.Open() -in dbTest.Program.Main(String[] args) location:blahblah...

【问题讨论】:

  • KeyNotFoundException?您确定此异常来自其他地方吗?
  • 我不知道为什么会这样。
  • 大概是因为某个键不在字典中。也许您可以分享整个错误(包括堆栈跟踪),或者我们应该只是猜测?
  • "unhandled exception:System.Collecions.Generic.KeyNotFoundException:key is not in the dictionary"将显示在控制台窗口中..
  • 阅读@David 的全部评论。更新您的问题以显示该异常的完整堆栈跟踪。

标签: c# mysql


【解决方案1】:

您指定的字符集无效。看看this reference

注意!使用小写值 utf8 而不是大写 UTF8,因为这会失败。

该值区分大小写,应为小写:

Database=weather;Server=127.0.0.1;Uid=root;Password=123456;pooling=false;CharSet=utf8;port=3306

诚然,这是一条无用的错误消息,但我认为它就是这样。 (这就是为什么阅读堆栈跟踪通常比消息本身更有帮助,因为它可以为研究答案提供基础。)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 2012-12-07
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
相关资源
最近更新 更多