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

来源:互联网 发布:js 与运算 编辑:程序博客网 时间:2024/04/29 13:08

之前几次碰到插入数据库的时候提示 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());


原创粉丝点击