码迷,mamicode.com
首页 > 数据库 > 详细

C#编码问题以及C#往Mysql插数据编码问题

时间:2017-01-19 00:42:38      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:nec   ati   com   root   分享   .com   www   get   编码转换   

技术分享
public static    string  get_uft8(string unicodeString)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
String decodedString = utf8.GetString(encodedBytes);
return decodedString;
}
技术分享

 

技术分享
   这边我以big5转换gb2312为例
Encoding big5 =Encoding.GetEncoding("big5");
Encoding gb2312 = Encoding.GetEncoding("gb2312");


byte[] big5b= big5.GetBytes("編程無悔!");
//关键也就是这句了
byte[] gb2312b= Encoding.Convert(big5,gb2312,big5b);

string strGb2312 = gb2312.GetString(gb2312b)
技术分享

 

1:在链接字符加入字符编码声明

<add key="mysqlconstr" value="UserId=root;Allow Zero Datetime=true;Charset=utf8;Host=125.*.*.*;Database=dbname;Password=123456"/>

 string connectiontext = "Server=139.222.313.153;Database=testsys;User=root;Password=1123456;Charset=utf8;";

向mysql说明我的字符编码是gb2312 或者 Utf-8, 不要搞错.

只要在连接MySQL时,正确地设定了字符集,无论数据库本身是使用什么格式编码的,都能得到正确的结果。也许有人会以为写数据时设定的字符集必需和读数据时一致,事实上完全没有必要。程序所要做的只是告诉 MySQL,目前操作MySQL使用的是什么字符集即可。因为MySQL会自动完成如下的转换工作:

写数据库时用的字符集-->存诸数据的字符集-->读取数据的字符集。

 

C#编码问题以及C#往Mysql插数据编码问题

标签:nec   ati   com   root   分享   .com   www   get   编码转换   

原文地址:http://www.cnblogs.com/x-poior/p/6298282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!