您的位置:首页 > 数据库 > MySQL

MySqlCommand查询条件中包含中文没有结果的问题

2016-01-22 16:24 666 查看
在写aps.net接口时,使用MySqlCommand执行查询语句select * from where name=’晓明’,数据库明明有’晓明’这条记录,但执行结果死活都是空,把数据库编码,程序编码都查了一遍没有找到问题,发现整个过程唯一不透明的地方就是MySqlCommand去执行查询的时候,因为不知道在这一层有没有对传入的中文字符做进一步处理,通过百度和google也没有找到有效的答案,于是去查MySqlCommand相关的属性和接口有没有和编码设置有关的,于是到MySQL官网去找,无意中发现一个页面有这样的介绍:

Important

If you are using multilanguage databases then you must specify the character set in the connection string. If you do not specify the character set, the connection defaults to the latin1 charset. You can specify the character set as part of the connection string, for example:

MySqlConnection myConnection = new MySqlConnection(“server=127.0.0.1;uid=root;” +

“pwd=12345;database=test;Charset=latin1;”);

页面url:http://dev.mysql.com/doc/connectors/en/connector-net-programming-connecting-errors.html

于是在数据库连接字符串中加入Charset=utf8,重新运行程序,问题解决!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: