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();
        }
    }
}

It will crash when running the sentence "mysql.Open()"

does anybody know why? the error message is: -unhandled exception:System.Collecions.Generic.KeyNotFoundException:the key is not in the dictionary -in System.Collections.Generic.Dictionary'2.get_Item(TKEY KEY) -in Mysql.Data.MySqlClient.CharSetMap.GetCharacterSet(DBVersion version,String CHARSETNAME) -in Mysql.Data.MySqlClient.CharSetMap.GetEncoding(DVversion version,String CharSetName) -in Mysql.Data.MySqlClient.Driver.Configure(MySqlConnection connection) -in Mysql.Data.MySqlClient.Mysqlconnection.Open() -in dbTest.Program.Main(String[] args) location:blahblah...

6
  • 3
    KeyNotFoundException? Are you sure this exception comes from somewhere else? Sep 19, 2014 at 12:06
  • I don't know why it happens.
    – Otwo
    Sep 19, 2014 at 12:09
  • 2
    Presumably because a key isn't in a dictionary. Perhaps you could share the entire error (including the stack trace), or should we just guess?
    – David
    Sep 19, 2014 at 12:09
  • "unhandled exception:System.Collecions.Generic.KeyNotFoundException:the key is not in the dictionary" will be shown in the console window..
    – Otwo
    Sep 19, 2014 at 12:10
  • 2
    Read @David's entire comment. Update your question to show the complete stack trace of that exception.
    – CodeCaster
    Sep 19, 2014 at 12:11

1 Answer 1

4

You're specifying an invalid character set. Take a look at this reference:

Note! Use lower case value utf8 and not upper case UTF8 as this will fail.

The value is case-sensitive and should be lowercase:

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

Admittedly it's an unhelpful error message, but it is what it is I suppose. (This is why reading a stack trace can often be more helpful than the message itself, since it can provide a basis for researching the answer.)

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.