.net操作mysql中文字符乱码的问题
//写入数据库时进行转换
public string GB2312_ISO8859(string write)
{
//声明字符集
System.Text.Encoding iso8859, gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
//国标2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] gb;
gb = gb2312.GetBytes(write);
//返回转换后的字符
return iso8859.GetString(gb);
}
//读出时进行转换
public string ISO8859_GB2312(string read)
{
//声明字符集
System.Text.Encoding iso8859, gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
//国标2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] iso;
iso = iso8859.GetBytes(read);
//返回转换后的字符
return gb2312.GetString(iso);
}
//批量数据转换
//其实就是将dataset的内容读出到xml文件,然后再输出
public DataSet ISO8859_GB2312(DataSet ds)
{
string xml;
xml = ds.GetXml();
ds.Clear();
//声明字符集
System.Text.Encoding iso8859, gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
//国标2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
byte[] bt;
bt = iso8859.GetBytes(xml);
xml = gb2312.GetString(bt);
ds.ReadXml(new System.IO.StringReader(xml));
return ds;
}