欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

解决Mysql插入中文乱码问题:Incorrect string value: ‘xA8Dx

程序员文章站 2022-04-11 21:29:44
...

之前几次碰到插入数据库的时候提示 Incorrect string value: ‘\xA8D\xA8D\xBA\xE1…’ for column ‘content’ at row 1 一般都是插入中文的时候就提示了、 解决办法是, 在插入数据库之前 先执行一次mysql_qeury(“set names gbk”) 确定好字符编码, 还有

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