C# mysql 读取 int32 奇怪的行为

本文关键字:mysql 读取 int32 | 更新日期: 2023-09-27 18:31:05

我有一个 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的价值,这太奇怪了?,我错在哪里?

C# mysql 读取 int32 奇怪的行为

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