c#串連mysql中文亂碼解決方案(MySql.Data.dll)

來源:互聯網
上載者:User

今天用C#類串連mysql資料庫出現中文亂碼具體解決方案如下:

 

用到的mysql表結構:

CREATE TABLE IF NOT EXISTS `tet` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

用到的mysql表資料:

INSERT INTO `tet` (`id`, `name`, `url`) VALUES
(1, '百度 ', 'http://www.baidu.com '),
(0, 'google ', 'http://www.google.com.hk '),
(3, '400電話 ', 'http://www.my400800.cn ');

 

 

一、查詢結果沒有問題,在查詢條件中如果輸入中文查詢不到內容,用insert測試為,插入的內容是亂碼

    

 

      首先在MySQL Command Line Client裡面輸入“SHOW VARIABLES LIKE 'character_set_%';”,可看到如下字元:

 

Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server latin1
character_set_system utf8
character_sets_dir C:\Program Files\VertrigoServ\Mysql\share\charsets\

 

可以用如下連接字串解決中文亂碼問題

User Id=test;Host=localhost;Database=eczhou;password=test;charset='gb2312'

 

二、檢索結果和插入內容都是亂碼

用MySQL Command Line Client裡面輸入“SHOW VARIABLES LIKE 'character_set_%';”,可看到如下字元:

 

Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/

 

即便是連接字串改成

User Id=test;Host=localhost;Database=eczhou;password=test;charset=utf8 檢索的結果還是亂碼

鬱悶中。。。。。。。

 

然後用  show variables like 'collation%';顯示結果是:

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

 

show variables like 'character%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

 

經過不懈努力的尋找,終於找到問題了,問題就出在上面紅色字型的編碼上,我的解決方案如下:

連接字串修改如下:

server=localhost;uid=root;pwd=root;database=test;Charset=latin1

 

c#代碼如下:

 /// <summary>
        /// 執行查詢語句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
            
                try
                {
                    connection.Open();
                    MySqlCommand M = new MySqlCommand("set names 'latin1'", connection);
                    M.ExecuteNonQuery();
                    //

                    MySqlDataAdapter da = new MySqlDataAdapter(SQLString, connection);
                    da.Fill(ds);
                }
                catch (MySqlException ex)
                {
                    connection.Close();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        dr[i] = Encoding.UTF8.GetString(Encoding.GetEncoding("latin1").GetBytes(dr[i].ToString()));

                    }

                }

                return ds;
            }
        }

 

一定要注意上面的紅色字型。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.