C#mysql读取int32奇怪的行为

时间:2013-09-13 03:15:53

标签: c# mysql

我有一个mysql表,例如

   CREATE TABLE `bs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ma_nguoitao` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `ma_nguoitao_dv` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `ma_nguoi_bilap` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `ma_nguoi_bilap_dv` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `ma_loi` int(11) DEFAULT NULL,
  `noi_dung` blob,
  `phan_hoi` blob,
  `ngay_lap` datetime DEFAULT NULL,
  `ket_luan` varchar(512) COLLATE utf8_bin DEFAULT NULL,
  `tinh_trang` mediumint(9) DEFAULT NULL,
  `ma_bill` varchar(16) COLLATE utf8_bin NOT NULL,
  `phat_chatluong` int(11) NOT NULL,
  `phat_denbu` int(11) NOT NULL,
  `PRIMARY KEY (`id`)
  `) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

我使用

保存了数据
             dbManager.com.CommandText = "INSERT INTO bs(`id`,`ma_nguoitao`,`ma_nguoitao_dv`,`ma_nguoi_bilap`,`ma_nguoi_bilap_dv`,`ma_loi`,`noi_dung`,`phan_hoi`,`ngay_lap`,`ket_luan`,`tinh_trang`,`ma_bill`,`phat_chatluong`,`phat_denbu`) "+
                "VALUES(?id, ?nt, ?ntdv, ?nbl, ?nbldv, ?ml, ?nd, ?ph, ?nl, ?kl, ?tt, ?mb, ?pcl, ?pdb )"+
                "ON DUPLICATE KEY UPDATE ma_loi=VALUES(`ma_loi`), noi_dung=VALUES(`noi_dung`), phan_hoi=VALUES(`phan_hoi`), ket_luan=VALUES(`ket_luan`), tinh_trang=VALUES(`tinh_trang`), phat_chatluong=VALUES(`phat_chatluong`), phat_denbu=VALUES(`phat_denbu`)";
                dbManager.com.Prepare();
                MySqlCommand temp = dbManager.con.CreateCommand();
                MySqlDataReader reader;
                temp.CommandText = "SELECT LAST_INSERT_ID();";
                temp.Prepare();

                foreach (BASE_BIENBAN_LOI_ENTITY obj in list)
                {
                    if (obj.ma_bill.Trim().Length > 0)
                    {
                        dbManager.com.Parameters.Add("?id", obj.id);
                        dbManager.com.Parameters.Add("?nt",obj.ma_nguoitao); 
                        dbManager.com.Parameters.Add("?ntdv", obj.ma_nguoitao_donvi);
                        dbManager.com.Parameters.Add("?nbl",obj.ma_nguoi_bi_lap);
                        dbManager.com.Parameters.Add("?nbldv", obj.ma_nguoi_bi_lap_donvi);
                        dbManager.com.Parameters.Add("?ml", obj.ma_loi);
                        dbManager.com.Parameters.Add("?nd", obj.noi_dung);
                        dbManager.com.Parameters.Add("?nl", System.DateTime.Now);
                        dbManager.com.Parameters.Add("?ph", obj.phan_hoi);
                        dbManager.com.Parameters.Add("?kl", obj.ket_luan);
                        dbManager.com.Parameters.Add("?tt", obj.tinh_trang);
                        dbManager.com.Parameters.Add("?mb", obj.ma_bill);
                        dbManager.com.Parameters.Add("?pcl", obj.phat_chatluong);
                        dbManager.com.Parameters.Add("?pdb", obj.phat_denbu);
                        dbManager.com.ExecuteNonQuery();
                        reader = temp.ExecuteReader();
                        if (reader.Read())
                        {
                            obj.id = (int)(reader.GetInt32(0));
                        }
                        reader.Close();
                    }
                }

obj.phat_chatluong定义为int。我的问题是,当我从phat_chatluong字段读取值时,该值似乎解释为ascii字节数组而不是int。例如,当我使用

读取记录时,记录为phat_chatluong=40
var cmd = new MySqlCommand("SELECT ma_bill ,id, ma_nguoitao, ma_nguoitao_dv,"+
                    "ma_nguoi_bilap, ma_nguoi_bilap_dv, ma_loi, noi_dung,"+
                    "phan_hoi, ngay_lap, ket_luan, tinh_trang, phat_chatluong, phat_denbu FROM bs_bienban", con);
            var reader = cmd.ExecuteReader();
            reader.Read();
            int phat_chatluong = (int) reader["phat_chatluong"];

然后我得到0x3430作为phat_chatluong的值,这太奇怪了?我哪里错了?

1 个答案:

答案 0 :(得分:1)

问题解决了!我工作的项目似乎使用旧的MySql连接器/驱动程序,现在可以在新版本的MySql连接器上使用。