【发布时间】:2018-01-03 11:52:06
【问题描述】:
当我从文本框 (txtMaterialUnitPrice,txtMaterialUnitPrice) 输入十进制数字时,例如 4,5 和 6,5,总价格应该是 29,25,但它是 29。这是我的代码。
private void btnSale_Click(object sender, EventArgs e)
{
salesTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
materialPrice = Decimal.Parse(txtMaterialUnitPrice.Text);
purchasedWeight = Decimal.Parse(txtMaterialUnitPrice.Text);
totalPrice = materialPrice * purchasedWeight;
curSales = Sales.Insert(customerID, customerNameSurname, customerPlate, materialID, materialType, materialPrice, purchasedWeight, totalPrice, salesTime, txtExplanation.Text);
LoadAll();
}
这是插入部分。
public static Sales Insert(int cID, String cNameSurname, String cPlate, int mID, String mName, Decimal mPrice, Decimal pWeight, Decimal totalPrice, String dt, String explanation)
{
String query = string.Format("INSERT INTO sales(CustomerID,CustomerNameSurname,CustomerPlate,MaterialID,MaterialType,MaterialPrice,PurchasedWeight,TotalPrice,SalesDate,Explanation) VALUES ('{0}', '{1}' , '{2}', '{3}','{4}','{5}','{6}','{7}','{8}','{9}')", cID, cNameSurname, cPlate, mID, mName, mPrice, pWeight, cleanAmount, dt, explanation);
MySqlCommand cmd = new MySqlCommand(query, DB.dbConn);
DB.dbConn.Open();
cmd.ExecuteNonQuery();
int id = (int)cmd.LastInsertedId;
Sales sale = new Sales(id, cID, cNameSurname, cPlate, mID, mName, mPrice, pWeight, totalPrice, dt, explanation);
DB.dbConn.Close();
return sale;
}
【问题讨论】:
-
不要使用字符串连接/格式化。改用参数
-
可能是因为 29,25 会被视为 2 个值.. 29 和 25。更不用说也与您的代码相关的 sql 注入概念
-
materialPrice = Decimal.Parse(txtMaterialUnitPrice.Text); purchasedWeight = Decimal.Parse(txtMaterialUnitPrice.Text);似乎是相同的文本框。是这个问题吗? -
正如@CamiloTerevinto 所说,使用参数来传递实际对象,而不是可能不是数据库预期格式的字符串表示。更不用说,因为里面有字符串,to avoid Bobby Tables.
-
什么是“cleanAmount”?您正在为 TotalPrice 列插入该值。