对不起我的英语..
当我在datagridview中插入记录与现有记录相同的描述但数量不同时...它为我插入的最后一条记录创建了一个新行......
我想要的是添加我插入到具有相同描述的旧记录的数量..
1. HERE'S THE IMAGE FOR YOU TO UNDERSTAND THE PROBLEM
结果应该是QUANTITY = 25 ..还有totalcbm .. totalcbm = quantity * cbm
private void btnSave_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO Inventory(Quantity,Unit,ItemCode,ItemName,Cbm,TotalCbm)VALUES(@Quantity,@Unit,@ItemCode,@ItemName,@Cbm,@TotalCbm)";
cmd.Parameters.AddWithValue("@Quantity", tbQuantity.Text.ToString());
cmd.Parameters.AddWithValue("@Unit", tbUnit.Text.ToString());
cmd.Parameters.AddWithValue("@ItemCode", tbItemCode.Text.ToString());
cmd.Parameters.AddWithValue("@ItemName", tbItemName.Text.ToString());
cmd.Parameters.AddWithValue("@Cbm", tbCbm.Text.ToString());
cmd.Parameters.AddWithValue("@TotalCbm", tbTotalCbm.Text.ToString());
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
frmUserAE form1 = new frmUserAE();
AccountForm.LoadGrid();
this.Hide();
}
}
}
答案 0 :(得分:0)
您正在执行INSERT语句,该语句将创建一个新行。
您应该执行UPDATE语句,具体取决于您认为的主键(描述?)。