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

C#将字符转换成utf8编码 GB321编码转换

 
复制代码
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:在连接字符加入字符编码声明html

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

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

向mysql说明个人字符编码是gb2312 或者 Utf-8, 不要搞错.数据库

只要在链接MySQL时,正确地设定了字符集,不管数据库自己是使用什么格式编码的,都能获得正确的结果。也许有人会觉得写数据时设定的字符集必需和读数据时一致,事实上彻底没有必要。程序所要作的只是告诉 MySQL,目前操做MySQL使用的是什么字符集便可。由于MySQL会自动完成以下的转换工做:

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

相关文章
相关标签/搜索