Q:

How to execute two queries at the same time in VB? (MySql)

What's the most efficient way to execute two queries at the same time?
I want to execute two INSERT sql queries (different tables) simultaneously when I click the submit button. is it possible?
Here's what I did:
    Dim conn As New MySqlConnection("Server = localhost; user id = root;password = ; database = ddap_hr")
    Dim sqlQuery1 As String = "INSERT INTO applicants VALUES ( '" & lblID.Text & "' , '" & txtLName.Text & "','" & txtFName.Text & "','" & txtMName.Text & "','" & cmboGender.Text & "','" & mtxtAge.Text & "','" & dtpBdate.Value & "','" & cmboStatus.Text & "','" & txtSSS.Text & "','" & txtTin.Text & "','" & txtReligion.Text & "','" & txtAddress.Text & "','" & mtxtContactNum.Text & "','" & txtEmail.Text & "')"
    Dim sqlQuery2 As String = "INSERT INTO appli_idgen(Lzero) VALUES ('" & lblNum.Text & "')"
    Dim cmd1 As New MySqlCommand(sqlQuery1)
    Dim cmd2 As New MySqlCommand(sqlQuery2)
    Dim rdr As MySqlDataReader

    Dim ConfirmMsg = MessageBox.Show("Are all the datas correct?" & Environment.NewLine & "   • Last Name:  " & txtLName.Text & Environment.NewLine & "   • First Name:  " & txtFName.Text & Environment.NewLine & "   • Middle Name:  " & txtMName.Text & Environment.NewLine & "   • Gender:  " & cmboGender.Text & Environment.NewLine & "   • Age:  " & mtxtAge.Text & Environment.NewLine & "   • Date of Birth:  " & dtpBdate.Text & Environment.NewLine & "   • Status:  " & cmboStatus.Text & Environment.NewLine & "   • SSS:  " & txtSSS.Text & Environment.NewLine & "   • TIN:  " & txtTin.Text & Environment.NewLine & "   • Religion:  " & txtReligion.Text & Environment.NewLine & "   • Address:  " & txtAddress.Text & Environment.NewLine & "   • Contact Number:  " & mtxtContactNum.Text & Environment.NewLine & "   • E-mail:  " & txtEmail.Text & Environment.NewLine, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, False)

    If ConfirmMsg = MsgBoxResult.Yes Then
        Try
            Try
                cmd1.Connection = conn
                conn.Open()
                cmd1.ExecuteNonQuery()
                rdr = cmd.ExecuteReader
                rdr.Read()
            Catch ex1 As MySqlException
                MsgBox(ex1.Message.ToString)
            Finally
                conn.Close()
            End Try

            Try
                cmd2.Connection = conn
                conn.Open()
                cmd2.ExecuteNonQuery()
                rdr = cmd.ExecuteReader
                rdr.Read()
            Catch ex2 As MySqlException
                MsgBox(ex2.Message.ToString)
            Finally
                conn.Close()
            End Try
        Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
        Finally
            Dim addAnother = MessageBox.Show("Do you want to add another applicant?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, False)
            If addAnother = MsgBoxResult.No Then
               Me.Close()
               Main_home.Refresh()
            End If
        End Try

    End If

I want to reduce the line of codes as much sa possible. I need your help. btw I'm using MySql. Sorry cause I'm new in VB. Thanks in advance.

A:

To answer the question you could try and start two threads running one immeddiately after the other. This is effectively having them run at the same time. 
Private Sub Button36_Click(sender As Object, e As EventArgs) Handles Button36.Click
    Dim t1 As New Threading.Thread(AddressOf Insert1)
    Dim t2 As New Threading.Thread(AddressOf Insert2)
    t1.Start()
    t2.Start()
End Sub

Private Sub Insert1()
    'do insert here
End Sub

Private Sub Insert2()
    'do insert here
End Sub

Whether you will get any performance improvement I doubt.

A:

It would help if you've shown us what you've tried, friend. 
As it stands, you can create a subroutine which will execute Nonquery to MySQL then CALL that sub TWICE on any trigger(button, checkbox, etc.):
Note: this requires you know how to use the MySQL connector for .NET You can look for more on that here
   Public Sub MyNonQuery(ByVal SQCommand as String)
      Dim conn As New MySqlConnection("server=your server ip; port=the server port; uid='your user id';password ='your password';")

      Dim SQLCMD as new MySqlCommand(SQCommand, conn)

      conn.open()
      SQLCMD.ExecuteNonQuery()
      conn.close()
    End Sub

Now you can use this sub in the following manner(s):

CALL it twice
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    MyNonQuery("Insert into db.tbl1 values(...);")
    MyNonQuery("Insert into db.tbl2 values(...);")

 End Sub

On a single call, Send TWO SQL commands
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    MyNonQuery("Insert into db.tbl1 values(...); Insert into db.tbl2 values(...);")

 End Sub

Remember to properly Clean the data from the user though, to prevent SQL Injection attacks. Ultimately I would suggest you look into studying Stored Procedures

