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

解决Mysql插入中文乱码问题:Incorrect string value: ‘\xA8D\xA8D\xBA\xE1…’ for column

2013年03月09日 ⁄ 综合 ⁄ 共 622字 ⁄ 字号 评论关闭

之前几次碰到插入数据库的时候提示 Incorrect string value: ‘\xA8D\xA8D\xBA\xE1…’ for column ‘content’ at row 1

一般都是插入中文的时候就提示了、

解决办法是, 在插入数据库之前 先执行一次mysql_qeury(“set names gbk”) 确定好字符编码,

还有可能要选择数据库校对码。

-----------------------------------------------------

C#代码:

            MySQLConnection conn = new MySQLConnection("Data Source=testdb;Password=123456;User ID=root;Location=localhost;Port=3306;charset=gbk");
            MySQLCommand cmdSet = new MySQLCommand("set names gbk", conn);
            MySQLCommand cmd = new MySQLCommand("INSERT INTO textTest(D_txt) VALUES ('" + textBox1.Text + "')", conn);
            conn.Open();
            cmdSet.ExecuteNonQuery();
            int result = (int)cmd.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show(result.ToString());

抱歉!评论已关闭.