vb.net 从VB.Net中的SQL表中删除记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13809510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete records from SQL table in VB.Net
提问by Joy1979
I am trying to delete records from my table but unable to do so. I have window form with Insert, Update, Delete and Save button. I can insert data but I can't delete specific or any records from that table. Below is my code.
我试图从我的表中删除记录,但无法这样做。我有带有插入、更新、删除和保存按钮的窗体。我可以插入数据,但不能从该表中删除特定或任何记录。下面是我的代码。
Whenever I click 'delete' button I get ("Error while deleting record on table..." & ex.Message, "Delete Records") msgbox only.
每当我单击“删除”按钮时,我只会收到(“删除表上的记录时出错...”和例如消息,“删除记录”)msgbox。
Am I missing something? Please advise. Any help would be great. Thank you!
我错过了什么吗?请指教。任何帮助都会很棒。谢谢!
Private Sub Deletebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deletebtn.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Server=USRAG-L-0067215\SQLEXPRESS;Database=Alamo Products_Design Data;Trusted_Connection=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "Delete From Design_Parameters where DesignID= ? "
cmd.ExecuteNonQuery()
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
con.Close()
End Try
End Sub
End Class
结束类
回答by Coding Duchess
you need to add parameter for DesignID
您需要为 DesignID 添加参数
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Server=USRAG-L-0067215\SQLEXPRESS;Database=Alamo Products_Design Data;Trusted_Connection=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "Delete From Design_Parameters where DesignID= @DesignID"
cmd.Parameters.Add(New SqlParameter("@DesignID", yourvaluehere))
cmd.ExecuteNonQuery()
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Catch ex As Exception
MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")
Finally
con.Close()
End Try