当我将数据网格视图行中的值更新到数据库时,我遇到了问题。问题是我在DB中设计了表格,其中某些字段为" bit"用于存储布尔值标志的数据类型。
当我将数据表分配给datagridview时,系统会将这些特定字段显示为复选框,这对我来说很合适。
但是当我尝试将这些值更新回数据库时,布尔值变为香蕉......这是我的代码......
int fragileChk = (Convert.ToBoolean(aRow.Cells[12].Value) ? 1 : 0);
int inflamChk = (Convert.ToBoolean(aRow.Cells[13].Value) ? 1 : 0);
int biologicalChk = (Convert.ToBoolean(aRow.Cells[15].Value) ? 1 : 0);
int emergencyChk = (Convert.ToBoolean(aRow.Cells[16].Value) ? 1 : 0);
int usedChk = (Convert.ToBoolean(aRow.Cells[25].Value) ? 1 : 0);
int offerChk = (Convert.ToBoolean(aRow.Cells[27].Value) ? 1 : 0);
string err;
string sqlComm = "UPDATE [70_warehouse_lines] SET " +
"ProductDescr = '" + aRow.Cells[5].Value.ToString() + "', " +
"PartNumber = '" + aRow.Cells[6].Value.ToString() + "', " +
"SerialNumber = '" + aRow.Cells[7].Value.ToString() + "', " +
"Quanitity = " + aRow.Cells[8].Value + ", " +
"Weight = " + aRow.Cells[10].Value + ", " +
"FragileFlag = " + fragileChk + ", " +
"InflammableFlag =" + inflamChk + ", " +
"BiologicalFlag = " + biologicalChk + ", " +
"EmergencyFlag = " + emergencyChk + ", " +
"SpecialInstructions = '" + aRow.Cells[17].Value.ToString() + "', " +
"ShopCostPrice = " + aRow.Cells[19].Value + ", " +
"RetailPrice1 = " + aRow.Cells[20].Value + ", " +
"RetailPrice2 = " + aRow.Cells[21].Value + ", " +
"WholePrice1 = " + aRow.Cells[22].Value + ", " +
"WholePrice2 = " + aRow.Cells[23].Value + ", " +
"CalculatedPrice = " + aRow.Cells[24].Value + ", " +
"UsedParts = " + usedChk + ", " +
"TimesProcessed = " + aRow.Cells[26].Value + ", " +
"OnOffer = " + offerChk + ", " +
"NotesPerPart = '" + aRow.Cells[28].Value.ToString() + "' " +
"WHERE WarehouseLineID = '" + aRow.Cells[0].Value.ToString() + "'";
myConn.ExecSqlCmd(sqlComm, out err);
任何想法? (我已声明int值仅用于诊断目的。请提前感谢您的帮助。
答案 0 :(得分:0)
始终使用参数化查询,使用参数有助于在数据库与程序接口结合使用时防止SQL注入攻击。 您也可以在参数化查询中指定数据类型,这将有助于您的情况。
string sqlComm = "UPDATE [70_warehouse_lines] SET " +
"ProductDescr = @ProductDescr " +
"PartNumber = @PartNumber " +
"SerialNumber = @SerialNumber " +
"Quanitity = @Quanitity" +
"Weight = @Weight" +
"FragileFlag = @FragileFlag" +
"InflammableFlag = @InflammableFlag" +
"BiologicalFlag = @BiologicalFlag" +
"EmergencyFlag = @EmergencyFlag" +
"SpecialInstructions = @SpecialInstructions " +
"ShopCostPrice = @ShopCostPrice" +
"RetailPrice1 = @RetailPrice1" +
"RetailPrice2 = @RetailPrice2 " +
"WholePrice1 = @WholePrice1 " +
"WholePrice2 = @WholePrice2 " +
"CalculatedPrice = @CalculatedPrice " +
"UsedParts = @UsedParts " +
"TimesProcessed = @TimesProcessed " +
"OnOffer = @OnOffer " +
"NotesPerPart = @NotesPerPart" +
"WHERE WarehouseLineID = @WarehouseLineID ";
MySqlCommand cmd = new MySqlCommand(sqlComm);
cmd.Parameters.Add("@FragileFlag", MySqlDbType.Bit).Value = (Convert.ToBoolean(aRow.Cells[12].Value) ? 1 : 0);
cmd.Parameters.Add("@InflammableFlag", MySqlDbType.Bit).Value=(Convert.ToBoolean(aRow.Cells[13].Value) ? 1 : 0);
cmd.Parameters.Add("@BiologicalFlag", MySqlDbType.Bit).Value=(Convert.ToBoolean(aRow.Cells[15].Value) ? 1 : 0);
cmd.Parameters.Add("@EmergencyFlag", MySqlDbType.Bit).Value = (Convert.ToBoolean(aRow.Cells[16].Value) ? 1 : 0);
....................................
....................................
....................................
and so on
cmd.ExecuteNonQuery();