2014-09-19 116 views
0
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()” 时,它会崩溃为什么当我使用MysqlConnection.open()时它不起作用?

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

+3

'KeyNotFoundException'?你确定这个异常来自其他地方吗? – 2014-09-19 12:06:04

+0

我不知道为什么会发生。 – Otwo 2014-09-19 12:09:22

+2

大概是因为密钥不在字典中。也许你可以分享整个错误(包括堆栈跟踪),或者我们应该猜测? – David 2014-09-19 12:09:26

回答

4

您正在指定一个无效的字符集。看看this reference

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

值是区分大小写的,应该是小写:

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

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

+0

检查复选标记后:http://meta.stackoverflow.com/questions/267818/stack-overflow-helped-me-with-my-problem-on-which-network-can-i-share-my-succes 。 – 2014-09-19 12:26:13

相关问题