c#向Mysql中插入数据乱码?
c#向Mysql中插入数据乱码。大家看一下代码。
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "emarket", "root", "root").AsString);
conn.Open();
MySQLCommand commn2 = new MySQLCommand("set names gb2312", conn);
commn2.ExecuteNonQuery();
try
{
conn.Open();
int i;
i = dt.Rows.Count;
int count = 0;
for (int j = 1; j < i; j++) //循环插入数据库的
{
string GOODS_CODE; // 商品编号
string GOODS_NAME; // 商品名称
GOODS_CODE = dt.Rows[j][0].ToString();
GOODS_NAME = dt.Rows[j][1].ToString();
string insertcmd1 = "insert into goods(GOODS_CODE,GOODS_NAME)";
insertcmd1 = insertcmd1 + "values ('" + GOODS_CODE + "','" + GOODS_NAME + "')"; //赋值写入数据库
MySQLCommand commn1 = new MySQLCommand(insertcmd1, conn);
commn1.ExecuteNonQuery();
commn1.Dispose();
在数据库里查看插入的数据是中文乱码? 怎么办?
Mysql 为 utf-8 。
--------------------编程问答-------------------- Server=LocalHost;Database=simkbp;User ID=root;Password=123;Charset=utf8;
你在数据库连接后面加上“Charset=utf8” 就不会乱码了 --------------------编程问答--------------------
这样写法怎么加
conn = new MySQLConnection(new MySQLConnectionString("localhost", "emarket", "root", "root").AsString); --------------------编程问答-------------------- 你不用参数构造 通过属性来连接数据库不就完了... --------------------编程问答-------------------- 连接字符串里加~Charset=utf8; --------------------编程问答-------------------- 是呀... --------------------编程问答--------------------
老大,帮我写一下。 我是超级菜鸟。 --------------------编程问答-------------------- conn = new MySQLConnection();
conn.ConnectionString="Server=LocalHost;Database=simkbp;User ID=root;Password=123;Charset=utf8;"
晕倒...他的属性是不是这个 我不太清楚 你看下那个MySQLConnection的连接数据库的属性是哪个 --------------------编程问答-------------------- 在连接字符串中加Charset=utf8就ok --------------------编程问答--------------------
这个不行。 --------------------编程问答--------------------
那你就这样吧 MySQLConnection conn = new MySQLConnection("Data source=localhost;Database=simkbp;User ID=root;Password=123;Charset=utf8;");
数据库用户名 密码 这些参数自己重新设下... --------------------编程问答--------------------
不支持这么写。用的 MySQLDriverCS 引用操作 Mysql 数据库的 --------------------编程问答-------------------- 那你看下有没哪个属性是连接数据库的 --------------------编程问答-------------------- 既然你的MySQL是UTF-8的字符集,已经支持中文,为什么你还要去改成GB2312呢?
建议你把那句“set names gb2312”去掉试试。 --------------------编程问答-------------------- 注意mysql 的字符集要设置为gbk 或者gb2312.
看我的百度空间一篇文章有介绍:http://hi.baidu.com/chengzq11/blog/item/20ae0dca8d6d5a4cf31fe76b.html --------------------编程问答-------------------- OK,已经解决了,原来是没加“Charset=utf8”啊,哈哈哈,谢谢啦。
补充:.NET技术 , C#