日期:2014-05-18  浏览次数:21437 次

asp.net gridview绑定en_US.819数据库乱码
asp.net gridview绑定en_US.819数据库乱码
不用gridview时,通过编码转换中文不会有乱码,gridview却试了多次仍然有乱码。

  using IBM.Data.Informix;

  this.GridViewSQLResult.DataSource = cmd.ExecuteReader();
  this.GridViewSQLResult.DataBind();
   

  protected void GridViewSQLResult_DataBound(object sender, EventArgs e)
  {
  int cols = this.GridViewSQLResult.HeaderRow.Cells.Count;
  for (int i = 0; i < this.GridViewSQLResult.Rows.Count; i++)
  {
  GridViewRow row = this.GridViewSQLResult.Rows[i];

  for (int col = 0; col < cols; col++)
  {
  this.txtSQLResult.Text = this.txtSQLResult.Text + 
  Environment.NewLine + row.Cells[col].Text;
  row.Cells[col].Text = GetEncodingString_Request(row.Cells
  [col].Text);
  this.txtSQLResult.Text = this.txtSQLResult.Text + 
  Environment.NewLine + row.Cells[col].Text;
  }

  }
  }


  /*iso-8859-1->gb2312*/
  /*具体原因我推测是因为数据过来是ISO-8859-1,但.Net地层格处理成了Gb2312了,
  * 所以需要通过两种编码转换成Unicode才能正常显示,
  * 听说在Java中只要new String(s.getBytes("ISO-8859-1"),"gb2312");就OK了。 */
  public static string GetEncodingString(string srcString)
  {
  Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1");
  Encoding gb2312Encode = Encoding.GetEncoding("gb2312");
  Encoding UnicodeEncode = Encoding.Unicode;
  byte[] srcBytes = e8859Encode.GetBytes(srcString);//用iso-8859-1去转换源字符串:源
  byte[] dstBytes = Encoding.Convert(gb2312Encode, UnicodeEncode, srcBytes);//但是,是从gb2312转到unicode的
  char[] dstChars = new char[UnicodeEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
  UnicodeEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);
  return new string(dstChars);

  }

------解决方案--------------------
哎 这么长 要看哈哦
------解决方案--------------------
SQL查询分析器,查出来是乱码?那就和.net 没有关系了阿

如果只是 .net出现乱码,可以尝试将aspx叶面保存为UTF-8,web.config的编码格式也设为UTF-8看看吧
------解决方案--------------------
可以试试楼上的
------解决方案--------------------
怎么这么乱啊。。
------解决方案--------------------
所有的数据存入byte数组,即使是读取出来的时候也统一放入byte数组再进行读取,我估计是你转换成gb2312的时候,输出显示的时候出的错,所以即便是gb2312也再次转换存入byte数组输出
------解决方案--------------------
不懂 UP~~~
------解决方案--------------------
探讨
所有的数据存入byte数组,即使是读取出来的时候也统一放入byte数组再进行读取,我估计是你转换成gb2312的时候,输出显示的时候出的错,所以即便是gb2312也再次转换存入byte数组输出

------解决方案--------------------
up