2015-05-15 92 views
-1
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中顯示任何錯誤,但是當我運行代碼時,我想要刪除的字段未被刪除。從MySQL數據庫那麼刪除的數據不會被刪除,VB.Net代碼

+0

如果有例外情況,您將丟棄*告訴您的所有信息*出了什麼問題。修復Catch塊以找出錯誤。 – Plutonix

+0

做到這一點,顯然我需要定義「@questionID」,我會着手解決這個問題。 – GiordySays

+0

因爲我已經添加了'MsgBox(「Error:」&ex.ToString())',所以在第11行中有一個錯誤,它是'Return comm.ExecuteNonQuery()'。 VS2013希望我定義前面提到過的@ questionID。顯然我錯過了一個return語句。 – GiordySays

回答

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)