如何在产品中重复时添加库存和总价?

时间:2015-03-12 09:08:15

标签: c#

    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());
        }
    }

1 个答案:

答案 0 :(得分:0)

即使不清楚,我也会尝试一下。您希望通过将库存和总价值添加到已有值来更新表。

然后这应该有效:

string sql = @"update inventoryOut set 
                  Model=@Model,
                  Stock=@Stock,
                  Date=@Date,
                  Price=Price + @Price,
                  TotalPrice=TotalPrice + @Total 
               where Product=@Product";
command.CommandText = sql;

除此之外,为什么对stringPrice等列使用Total?不要使用AddWithValueAdd并传递正确的SqlDbType。例如:

decimal price = decimal.Parse(txtPriceOut.Text);
command.Parameters.Add("@Price", MySqlDbType.Decimal).Value = price;