c#winform连接到mysql .Net Framework不支持“字符集'UTF8'。”

时间:2017-02-15 11:16:53

标签: c# mysql winforms

按下按钮时,我有一个连接到mysql的winform

 string sql = "INSERT INTO `products` (`catid`, `entity_id`, `sku`, `price`, `final_price`, `qty`, `is_in_stock`, `special_from_date`, `special_to_date`, `status`, `parent_id`) VALUES ('@catid', 1,'100',100.0000,    50.0000,9.0000, 1,  NULL,   NULL,   NULL,   NULL);";
            if  (mproductDataGridView.SelectedRows.Count != 0)
            {

                //
                string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=Abcd1234;database=pos; convert zero datetime=True;charset=UTF8";
                MySqlConnection databaseConnection = new MySqlConnection(connectionString);
                databaseConnection.Open();
                // MySqlConnection databaseConnection = dao.connect();
                MySqlCommand sqlCmd = new MySqlCommand(sql, databaseConnection);

                //sqlCmd.Parameters.Add(new MySqlParameter("@catid", "abc"));


                foreach (DataGridViewRow r in mproductDataGridView.SelectedRows)
                {
                    //{ "catid":"15","entity_id":"231","sku":"msj000","name":"French Cuff Cotton Twill Oxford","price":"190.0000","final_price":"190.0000","qty":"14.0000","is_in_stock":"1","special_from_date":null,"special_to_date":null,"status":"1","parent_id":"402"
                    string str_catid = r.Cells["catid"].Value.ToString();
                    string entity_id = r.Cells["entity_id"].Value.ToString();
                    string sku = r.Cells["sku"].Value.ToString();
                    string name = r.Cells["name"].Value.ToString();
                    string price = r.Cells["price"].Value.ToString();
                    string final_price = r.Cells["final_price"].Value.ToString();
                    string qty = r.Cells["qty"].Value.ToString();
                    string is_in_stock = r.Cells["is_in_stock"].Value.ToString();
                    string special_from_date = r.Cells["special_from_date"].Value.ToString();
                    string special_to_date = r.Cells["special_to_date"].Value.ToString();
                    string status = r.Cells["status"].Value.ToString();
                    string parent_id = r.Cells["parent_id"].Value.ToString();

                    sqlCmd.Parameters.AddWithValue("@catid", str_catid);
                    sqlCmd.ExecuteNonQuery();
                    //dao.insertCommand(sqlCmd);


                }

抛出一个例外:

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

Additional information: Character set 'UTF8' is not supported by .Net Framework.

似乎连接字符串不能包含charset = UTF8,否则会抛出异常。另一个项目在连接字符串中也使用charset = UTF8但是 没有错误,任何人都知道这是什么问题?

0 个答案:

没有答案