111,119
社区成员
发帖
与我相关
我的任务
分享public static MySqlConnection conn = new MySqlConnection("server=localhost;UserId=root;password=mypsd;Database=ccc;charset=latin1;connection timeout=3600;");
public static MySqlDataReader BindReader(string sqlStr)
{
try
{
conn.Open();
MySqlCommand sqlCmd = new MySqlCommand("set names latin1;"+sqlStr, conn);
MySqlDataReader sdr = sqlCmd.ExecuteReader();
return sdr;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}

private string changeEncoding(string read){
System.Text.Encoding iso8859,gb2312;
iso8859 = System.Text.Encoding.GetEncoding("latin1");
gb2312 = System.Text.Encoding.GetEncoding("utf-8");
byte[] iso;
iso = iso8859.GetBytes(read);
return gb2312.GetString(iso);
}

public static MySqlConnection conn = new MySqlConnection("server=localhost;UserId=root;password=mypsd;Database=ccc;charset=latin1;connection timeout=3600;charset=gb2312"); public string GetEncodingString(string srcString)
{
Encoding utf= Encoding.UTF8;
Encoding lat = Encoding.GetEncoding("latin1");
byte[] lbs =lat.GetBytes(srcString);
MessageBox.Show(lbs.Length.ToString()); //结果为96
byte[] cc = Encoding.Convert(lat, utf, lbs);
MessageBox.Show(cc.Length.ToString()); //结果为163
char[] dstChars = new char[utf.GetCharCount(lbs, 0, lbs.Length)];
utf.GetChars(lbs, 0, lbs.Length, dstChars, 0);
return new string(dstChars);
}

Encoding.UTF8.GetString(Encoding.GetEncoding("latin1").GetBytes(_DS32.GetString("setting_value")));mysql_query("set names utf8");
mb_internal_encoding('utf8'); 就可以正常显示。