这是我在vb中的代码:
cmd = New SqlCommand("INSERT INTO export (eDate, qty, stockID, empID, machineID, jobID, receiptNO, plateQty, eDesc) VALUES (@eDate, @qty, @stockID, @empID, @machineID, @jobID, @receiptNO, @plateQty, @eDesc)", con)
cmd.CommandTimeout = 1000
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@eDate", dateTXT.Text)
cmd.Parameters.AddWithValue("@qty", qtyTXT.Text)
cmd.Parameters.AddWithValue("@stockID", comboStock.SelectedValue)
cmd.Parameters.AddWithValue("@empID", comboEmp.SelectedValue)
cmd.Parameters.AddWithValue("@machineID", comboMachine.SelectedValue)
cmd.Parameters.AddWithValue("@jobID", comboJob.SelectedValue)
cmd.Parameters.AddWithValue("@receiptNO", receiptTXT.Text)
cmd.Parameters.AddWithValue("@plateQty", TextBox1.Text)
cmd.Parameters.AddWithValue("@eDesc", descTXT.Text)
cmd.ExecuteNonQuery()
If (cmd.ExecuteNonQuery) Then
cmd = New SqlCommand("UPDATE stock SET qty = qty - @qty WHERE id = '" & comboStock.SelectedValue & "'", con)
cmd.Parameters.AddWithValue("@qty", qtyTXT.Text)
cmd.ExecuteNonQuery()
MsgBox.Show("Successfuly saved!")
Else
MessageBox.Show("Not saved!")
End If
当我在导出表中插入记录时,我有两个表(导出和库存)它还应该更新库存项目,但我的问题是当我插入一个记录导出它时将它保存在表格副本上。 我该怎么做?
答案 0 :(得分:1)
您正在执行2次插入查询
cmd.ExecuteNonQuery()
If (cmd.ExecuteNonQuery) Then
如果要检查是否插入了某些内容,请移除顶部的ExecuteNonQuery()
并将If语句更改为此。
If (cmd.ExecuteNonQuery) > 0 Then
答案 1 :(得分:0)
这部分代码再次调用ExecuteNonQuery()
:
If (cmd.ExecuteNonQuery) Then
导致重复INSERT
:
SqlCommand("INSERT INTO export (eDate, qty, stockID, empID, machineID, jobID, receiptNO, plateQty, eDesc)
VALUES (@eDate, @qty, @stockID, @empID, @machineID, @jobID, @receiptNO, @plateQty, @eDesc)", con)
我个人会根据您的要求创建Insert trigger:
当我在导出表中插入记录时,它还应该更新库存项目
答案 2 :(得分:0)
cmd.ExecuteNonQuery() '//This will insert to the DB
'// I know you are only checking to see if the insert was successful, '
'// but you are essentially inserting again, then testing.'
If (cmd.ExecuteNonQuery) Then
...
...
End If
以下是避免重复insert
的一种方法。
Dim NumberOfRowsInserted = cmd.ExecuteNonQuery()
'// now this variable will hold either 1 if the insert command was successful'
'// or 0 if the insert failed'
If NumberOfRowsInserted > 0 Then
'// do whatever'
...
...
...
End If