那么删除MySQL数据库中的数据不会被删除,VB.Net代码

时间:2015-05-15 16:02:00

标签: mysql vb.net

Public Class Remove_Text_Based
Public Function ParameterizedNonQueryCommand(ByRef NonQuery As String, ByVal Parameters As List(Of MySqlParameter), Optional ByVal Connection As MySql.Data.MySqlClient.MySqlConnection = Nothing) As Integer
    Dim comm As New MySql.Data.MySqlClient.MySqlCommand(NonQuery, Connection)
    Try
        For Each param As MySql.Data.MySqlClient.MySqlParameter In Parameters
            comm.Parameters.Add(param)
        Next
        If comm.Connection.State <> ConnectionState.Open Then comm.Connection.Open()
        comm.Prepare()
        Return comm.ExecuteNonQuery()
    Catch ex As Exception
        Return CType(False, Integer)
    Finally
        comm.Connection.Close()
        comm.Dispose()
    End Try
End Function

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    Dim query As String = "DELETE FROM textquestion WHERE questionID = @questionID"
    Dim params As New List(Of MySqlParameter)
    params.Add(New MySqlParameter("question", 1))
    Dim conn As MySqlConnection = New MySqlConnection("server=localhost;user=root;password=password;database=giordydatabase")
    ParameterizedNonQueryCommand(query, params, conn)

    'error messages for unfilled boxes

    If QuestionIDBox.Text = "" Then
        MessageBox.Show("Please enter the questionID please")
    End If
    Dim submit As Integer = MessageBox.Show("Are you sure you want to submit the user information", "Submit", MessageBoxButtons.YesNo)
    If submit = DialogResult.No Then
        MessageBox.Show("The user information will not be sumbitted")
    ElseIf submit = DialogResult.Yes Then
        MessageBox.Show("The user information has been submitted")
    End If
End Sub

上面是我创建的用于删除数据库字段的代码,我创建了一个函数,以及我的查询删除代码的子代。我的问题是代码在VS2013中没有显示任何错误,但是当我运行代码时,我想要删除的字段没有被删除。

1 个答案:

答案 0 :(得分:0)

Public Class Remove_Text_Based

Public Function ParameterizedNonQueryCommand(ByRef NonQuery As String, ByVal Parameters As List(Of MySqlParameter), Optional ByVal Connection As MySql.Data.MySqlClient.MySqlConnection = Nothing) As Integer
    Dim comm As New MySql.Data.MySqlClient.MySqlCommand(NonQuery, Connection)
    Try
        For Each param As MySql.Data.MySqlClient.MySqlParameter In Parameters
            comm.Parameters.Add(param)
        Next
        If comm.Connection.State <> ConnectionState.Open Then comm.Connection.Open()
        comm.Prepare()
        Return comm.ExecuteNonQuery()
    Catch ex As Exception
        Return CType(False, Integer)
        MsgBox("Error: " & ex.ToString())
    Finally
        comm.Connection.Close()
        comm.Dispose()
    End Try
End Function

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    Dim query As String = "DELETE FROM textquestion WHERE questionID = " & QuestionIDBox.Text & ""
    Dim params As New List(Of MySqlParameter)
    params.Add(New MySqlParameter("questionID", 1))
    Dim conn As MySqlConnection = New MySqlConnection("server=localhost;user=root;password=password;database=giordydatabase")
    ParameterizedNonQueryCommand(query, params, conn)