Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当库存和总价在产品中重复时,如何添加库存和总价?_C# - Fatal编程技术网

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

C# 当库存和总价在产品中重复时,如何添加库存和总价?,c#,C#,即使不清楚,我也要试一试。您希望通过将stock和totalprice添加到已经可用的值来更新表 那么这应该是可行的: private void btnSaveOut_Click(object sender, EventArgs e) { double tps = Convert.ToDouble(txtStockOut.Text) * Convert.ToDouble(txtPriceOut.Text); try {

即使不清楚,我也要试一试。您希望通过将stock和totalprice添加到已经可用的值来更新表

那么这应该是可行的:

    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;