国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

asp.net使用Mysql亂碼處理

2024-07-24 12:31:19
字體:
來源:轉載
供稿:網友
   在asp.net與mysql數據庫打交道的時候,由于配置的問題,會遇到自己寫的方法在讀取數據庫中數據的時候,英文,數字可以正常通過,但是中文就無法通過,以登錄為例(方法略),當輸入英文用戶名的時候可以正常登錄,但輸入中午的時候方法就會報錯,解決方法如下:

  1,在 mysql數據庫安裝目錄下找到my.ini文件,把default-character-set的值修改為 default-character-set=gb2312(修改兩處),保存,重新啟動。

  2,找到asp.net文件的web.config文件,在</httpModules>標簽的后面添加<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>

  3,如果連接數據庫的字符串寫在web.config里面,則在連接字符串里面添加charset=gb2312(如:<add key="test" value="server=localhost;database=dbtest;uid=root;pwd=root;charset=gb2312"/>)

  4,重新編譯。

  有時還會遇到數據庫里面顯示的數據是中午,但讀取到頁面之后成了亂碼,而且在寫存儲工程的時候,有中文出現,編譯不會通過,我的解決方法是,我的解決方法是,把中文都轉化為十六進制,然后讀出來。代碼如下

  Mysql:

  drop procedure if exists proc_hm_holiday_top5;

  create procedure proc_hm_holiday_top5()

  begin

  select  *,(select userinfo_name from hm_userinfo where userinfo_id=holiday_uid) as username,

  case

  when holiday_type=1 and holiday_typeTwo=1 then _utf8 0xE8AFB7E581872FE79785E5818720

  when holiday_type=1 and holiday_typeTwo=2 then _utf8 0xE8AFB7E581872FE4BA8BE5818720

  when holiday_type=1 and holiday_typeTwo=3 then _utf8 0xE8AFB7E581872FE4B8A7E5818720

  when holiday_type=1 and holiday_typeTwo=4 then _utf8 0xE8AFB7E581872FE5A99AE5818720

  when holiday_type=1 and holiday_typeTwo=5 then _utf8 0xE8AFB7E581872FE4BAA7E5818720

  when holiday_type=1 and holiday_typeTwo=6 then _utf8 0xE8AFB7E581872FE5B9B4E5818720

  when holiday_type=1 and holiday_typeTwo=7 then _utf8 0xE8AFB7E581872FE585B6E4BB9620

  when holiday_type=2 then _utf8 0xE58AA0E78FAD

  when holiday_type=3 then _utf8 0xE587BAE5B7AE

  when holiday_type=4 then _utf8 0xE7A7BBE4BC91

  end

  as holiday_class from hm_holiday where 1=1 order by holiday_addTime desc limit 5;

  end;

  C#代碼(將中文轉化為十六進制)

  public  string GetHexFromChs(string s)

  {

  if ((s.Length % 2) != 0)

  {

  s += " ";//空ge

  }

  System.Text.Encoding chs = System.Text.Encoding.GetEncoding("utf-8");

  byte[] bytes = chs.GetBytes(s);

  string str = "";

  for (int i = 0; i < bytes.Length; i++)

  {

  str += string.Format("{0:X}", bytes[i]);

  }

  return str;

  }

  private void button1_Click(object sender, EventArgs e)

  {

  string shuru = textBox1.Text.Trim().ToString();

  string str = GetHexFromChs(shuru);

  textBox2.Text = str.Trim().Trim();

  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武胜县| 綦江县| 慈利县| 巴林左旗| 于田县| 高淳县| 邢台县| 福建省| 仲巴县| 喀什市| 桦川县| 开原市| 宜兰市| 泗阳县| 隆子县| 汽车| 缙云县| 务川| 玉屏| 开阳县| 普格县| 乐都县| 习水县| 民和| 清水县| 贡嘎县| 兰西县| 小金县| 阳东县| 常州市| 隆德县| 泽州县| 泸水县| 淮安市| 桦川县| 张北县| 搜索| 乌兰县| 名山县| 克山县| 象山县|