如何调试以下代码?
Dim deleteok As String = MsgBox("Do you really want to delete this record", MsgBoxStyle.YesNo, "updating records")
If vbYes Then
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "Delete From TB_stinfo where id=?"
cmd.Parameters.Add(New OleDbParameter("?", TextBox1.Text))
cmd.ExecuteNonQuery()
MessageBox.Show("one record is deleted")
Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
dt.Clear()
Form1_Load(sender, e)
clear()
checkcon()
End Try
Else
MsgBox("This record is not deleted")
End If
答案 0 :(得分:1)
我重新格式化了代码,因此它更具可读性:
Dim deleteok As String = MsgBox("Do you really want to delete this record", MsgBoxStyle.YesNo, "updating records")
If vbYes Then
Try
con.Open()
cmd.Connection = con cmd.CommandText = "Delete From TB_stinfo where id=?"
cmd.Parameters.Add(New OleDbParameter("?", TextBox1.Text))
cmd.ExecuteNonQuery()
MessageBox.Show("one record is deleted")
Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
dt.Clear()
Form1_Load(sender, e)
clear()
checkcon()
End Try
Else
MsgBox("This record is not deleted")
End If
我在这里看到的头号问题是,我们看不到您要删除的密钥值是什么,也不知道要删除的表中数据的格式。无法确定此代码究竟出现了什么问题。
我的建议是在这一行cmd.ExecuteNonQuery()
上设置一个断点,看看TextBox1.Text的值是什么,并确保它符合您的预期,查找前导/尾随空格或其他任何& #39;意想不到的......除此之外,在上下文方面还没有其他的东西可以继续。