-2

我正在尝试从我的表中删除记录,但无法这样做。我有带有插入、更新、删除和保存按钮的窗口窗体。我可以插入数据,但不能从该表中删除特定或任何记录。下面是我的代码。

每当我单击“删除”按钮时,我只会得到(“删除表上的记录时出错...”和 ex.Message,“删除记录”)msgbox。

我错过了什么吗?请指教。任何帮助都会很棒。谢谢!

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

结束类

4

1 回答 1

2

您需要为 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
于 2012-12-10T21:26:24.577 回答