大家好, 我做一个任务,我想要执行更新查询我的表名是Medicine_Available_Detail这里iam存储总药物可用列名是可用性,我有一个datagridview,其中我把药物的数量在用户输入特定药物的数量之后然后它表更新作为该药物的可用性 - 数量我写了代码但是没有执行 我的尝试:
int quantity = Convert.ToInt16(dataGridView1.Rows [e.RowIndex] .Cells [ Quantity]值)。 cmd = new OleDbCommand( @ update Medicine_Available_Detail set [可用性] = [可用性] - @ Quantity,其中Medicine_Name = @ Medicine_Name,con); cmd.Parameters.AddWithValue( @ Quantity,quantity); cmd.Parameters.AddWithValue( @ Medicine_Name,medicinename); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show( 记录成功更新);解决方案
首先,要知道是否有任何行被更新,请检查变量的值 n
如果为0则表示没有记录符合您为更新定义的条件。 所以检查varible的值
medicinename 不存在。 通过查看你的代码,这是你的建议。你好这里是找到解决方案我使用两个cmd.ExecuteNonQuery();它对我来说很好,dataGridView1_RowLeave事件我写了3个插入查询和一个更新查询而只有一个ExecuteNonQuery而不是使用一个执行非查询我写了两个单独的ExecuteNonQuery与不同的变量 cmd = new OleDbCommand( update Medicine_Available_Detail set [可用性] = [可用性] - @ Quantity其中[Medicine_Name] = @ Medicine_Name,con); cmd.Parameters.AddWithValue( @ Quantity,quantity); cmd.Parameters.AddWithValue( @ Medicine_Name,medicinename); con.Open(); int n1 = cmd.ExecuteNonQuery(); con.Close();
Hi all, Iam doing one task where i want to executed update query i have table name is Medicine_Available_Detail here iam storing total medicine available column name is Availability,I have one datagridview where im putting Quantity of that Medicine after user enter Quantity of particular medicine then it table update as Availability-Quantity for that medicine i written a code but its not executed What I have tried:
int quantity = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value); cmd = new OleDbCommand(@"update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where Medicine_Name=@Medicine_Name", con); cmd.Parameters.AddWithValue("@Quantity", quantity); cmd.Parameters.AddWithValue("@Medicine_Name", medicinename); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Updated Successfully"); 解决方案 First of all, to know if any row is updated or not, check value of your variable nif it is 0 means no record is matching the criteria you are defining for update. So check the value of varible
medicinenamein your table exists on not. This is all can your suggest by looking at your code.
Hi here is find solution i use two cmd.ExecuteNonQuery();its working fine for me ,dataGridView1_RowLeave event i written 3 insert query and one update query and only one ExecuteNonQuery instead using one Execute non query i written two seprate ExecuteNonQuery with different variable cmd = new OleDbCommand("update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where [Medicine_Name]=@Medicine_Name", con); cmd.Parameters.AddWithValue("@Quantity", quantity); cmd.Parameters.AddWithValue("@Medicine_Name", medicinename); con.Open(); int n1 = cmd.ExecuteNonQuery(); con.Close();更多推荐
更新查询未正确执行
发布评论