0

I am new to VB.NET and as well as MySqL. I want to delete records in my database. Using,

    Dim SQLStatement As String = "DELETE name,date FROM people"
    RetrieveInfos(SQLStatement)

Where,

Public Sub RetrieveInfos(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
        .Connection = SQLConnection
        .ExecuteNonQuery()

    End With

    SQLConnection.Close()
    SQLConnection.Dispose()
End Sub

But there is an error of InvalidOperationException, I am confused. I want to delete records in my database without referencing the " WHERE number = VALUE " in order to delete the previous recorded value. Is that possible?

Any help would be appreciated. Thanks!

1
  • It seems that you need an update statement instead of delete statement. Commented Jul 27, 2012 at 2:20

1 Answer 1

1

You've got a few problems:

  1. You do not specify column names in the delete statement since you are deleting records.

  2. Your command, should it be successfully executed, will delete every record in the people table.

  3. You are closing and disposing the SQLConnection at the end of the RetrieveInfos method. If this connection is open elsewhere in your code, only the first execution of the code will work.

  4. Your method should not be called Retrieve if it is deleting.

  5. The command should be wrapped in a using statement

    Using cmd As MySqlCommand = New MySqlCommand
        With cmd
            .CommandText = SQLStatement
            .CommandType = CommandType.Text
            .Connection = SQLConnection
            .ExecuteNonQuery()
        End With
    End Using
    
Sign up to request clarification or add additional context in comments.

1 Comment

In addition: you are trying to drop the columns off of the table, are you not? This is not the right syntax to change the database schema. If you are trying to remove the actual values in the columns, this is also wrong. You need to UPDATE command for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.