1

I am using VB.NET with a MySQL database. I want to update this code to do it all in ONE SQL instead of THREE. Anyone know how?

Here's the code I'm using, works fine but too slow with multiple lines...

If count3 = "1" Then

    Dim myCommand As New MySqlCommand
    Dim myAdapter As New MySqlDataAdapter
    Dim SQL As String
    myCommand.Connection = conn
    myAdapter.SelectCommand = myCommand
    SQL = "UPDATE employees SET emprole1 = '" & val2 & "' WHERE emprole1 = '" & val1 & "'"
    myCommand.CommandText = SQL
    myCommand.ExecuteNonQuery()
    SQL = "UPDATE employees SET emprole2 = '" & val3 & "' WHERE emprole2 = '" & val2 & "'"
    myCommand.CommandText = SQL
    myCommand.ExecuteNonQuery()
    SQL = "UPDATE employees SET emprole3 = '" & val4 & "' WHERE emprole3 = '" & val3 & "'"
    myCommand.CommandText = SQL
    myCommand.ExecuteNonQuery()

End If

1 Answer 1

1

This is just my guess, but you could try gluing all three SQL statements together; pretty much like

SQL = "update employees set ... ; update employees set ... ;";

Note the semicolon that separates statements.

Sign up to request clarification or add additional context in comments.

4 Comments

I was able to do it like this:
SQL = "UPDATE employees SET emprole1 = '" & val2 & "' WHERE emprole1 = '" & val1 & "' ;" SQL &= "UPDATE employees SET emprole2 = '" & val3 & "' WHERE emprole2 = '" & val2 & "' ;" SQL &= "UPDATE employees SET emprole3 = '" & val4 & "' WHERE emprole3 = '" & val3 & "'
@Boomgirl please click to accept the answer as correct if it solved your problem.
I really recommend you also use parameterized queries, instead of doing things like emprole = " & var, that is exactly what SQL injection attacks rely on. See here for how its done :-) stackoverflow.com/questions/652978/…

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.