C# 当库存和总价在产品中重复时,如何添加库存和总价?
即使不清楚,我也要试一试。您希望通过将stock和totalprice添加到已经可用的值来更新表 那么这应该是可行的:C# 当库存和总价在产品中重复时,如何添加库存和总价?,c#,C#,即使不清楚,我也要试一试。您希望通过将stock和totalprice添加到已经可用的值来更新表 那么这应该是可行的: private void btnSaveOut_Click(object sender, EventArgs e) { double tps = Convert.ToDouble(txtStockOut.Text) * Convert.ToDouble(txtPriceOut.Text); try {
private void btnSaveOut_Click(object sender, EventArgs e)
{
double tps = Convert.ToDouble(txtStockOut.Text) * Convert.ToDouble(txtPriceOut.Text);
try
{
MySqlConnection con = new MySqlConnection("Server=localhost;UID=root;Database=db_cignal");
con.Open();
MySqlCommand command = con.CreateCommand();
command.CommandText = "update inventoryOut set Model=@Model,Stock=@Stock,Date=@Date,Price=@Price,TotalPrice=@Total where Product=@Product";
command.Parameters.AddWithValue("@Product", cbProductOut.SelectedItem.ToString());
command.Parameters.AddWithValue("@Model", txtModelOut.Text);
command.Parameters.AddWithValue("@Stock", txtStockOut.Text);
command.Parameters.AddWithValue("@Date", DateTime.Now);
command.Parameters.AddWithValue("@Price", txtPriceOut.Text);
command.Parameters.AddWithValue("@Total", tps.ToString());
command.Prepare();
command.ExecuteNonQuery();
BindGridInventoryOut();
ClearFieldsinInventoryOUT();
con.Close();
MessageBox.Show("Saved!");
}
catch (Exception r)
{
MessageBox.Show(r.ToString());
}
}
除此之外,为什么对Price
或Total
等列使用string
?不要使用AddWithValue
而是Add
并传递正确的SqlDbType
。例如:
string sql = @"update inventoryOut set
Model=@Model,
Stock=@Stock,
Date=@Date,
Price=Price + @Price,
TotalPrice=TotalPrice + @Total
where Product=@Product";
command.CommandText = sql;
我不明白你的问题。你能澄清一下吗?嗯,考虑到库存中的产品是主要的。我想做的是每当我点击保存按钮时,我想更新记录,如果我想更新数据库中已经存在的记录,我想添加它的库存、总价,并更改日期。oww感谢Soner的建议:)看看
合并
。听起来你可以用这个:)
decimal price = decimal.Parse(txtPriceOut.Text);
command.Parameters.Add("@Price", MySqlDbType.Decimal).Value = price;