为什么当我使用 MysqlConnection.open() 时它不起作用? [英] Why it doesn't work when I use MysqlConnection.open()?

查看:56
本文介绍了为什么当我使用 MysqlConnection.open() 时它不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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()"时会崩溃

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

有人知道为什么吗?错误信息是:- 未处理的异常:System.Collections.Generic.KeyNotFoundException:键不在字典中-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 连接)-in Mysql.Data.MySqlClient.Mysqlconnection.Open()-in dbTest.Program.Main(String[] args) location:blahblah...

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...

推荐答案

您指定的字符集无效.看看这个参考:

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

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

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.)

这篇关于为什么当我使用 MysqlConnection.open() 时它不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆