现在的位置: 首页 > 综合 > 正文

MySql字符乱码

2011年01月20日 ⁄ 综合 ⁄ 共 669字 ⁄ 字号 评论关闭

问题:VS2005开发的窗体应用程序,数据库为MySql.net;查询带有中文的语句,没有返回结果,也没有报错!!!

就是MySql字符编码的问题啊!!!

解决办法:

1、用MySQLDriverCS
在conn.open();后
先执行:
MySQLCommand cmd=new MySQLCommand("set charset gb2312",conn);
//或者是 MySQLCommand cmd=new MySQLCommand("set charset gbk",conn);也可以.我都试过.
cmd.ExecuteNonQuery();
cmd.Dispose();
再执行你的其它语句,即可。

2、用MySQL Connector/Net
在连接字符串后面加上charset=gb2312;

            string connctionstr = "server=192.168.1.121;user id=shide;password=shide;persist security info=True;database=shidevideo;port=3307;charset=gb2312";
            MySqlConnection con = new MySqlConnection(connctionstr);
            MySqlDataAdapter dap = new MySqlDataAdapter(msg, con);
            DataSet ds2 = new DataSet();
            dap.Fill(ds2);

【上篇】
【下篇】

抱歉!评论已关闭.