title: Checking which row from DataGridView exists in MySQL takes too long
body: <p>I'm executing a query which returns me around 400 records (orders) and I display them in DataGridView. Then I have to check each row and make a green every row which exists in other MySQL database (by ID). I think I'm not doing this optimally. Here is what I am doing:</p>
  
  <pre><code>For Each oRow As DataGridViewRow In dgv.Rows
         Dim orderNumber As Integer = oRow.Cells(0).Value
  
         Dim exist As Boolean = mySql.CheckIfExists(shop, orderNumber)
  
         If Not exist Then
               Continue For
         End If
  
         If exist Then
               oRow.DefaultCellStyle.BackColor = Color.LightGreen
               oRow.DefaultCellStyle.SelectionBackColor = Color.Green
               oRow.DefaultCellStyle.SelectionForeColor = Color.LightCoral
               Continue For
         End If
  Next
  </code></pre>
  
  <p>Here is <code>CheckIfExists()</code> method in <code>MySQL</code> class:</p>
  
  <pre><code>Public Function CheckIfExists(ByVal shop As String, ByVal orderNumber As Integer) As Boolean
  
          Dim dt As New DataTable
          Dim sql As String = "" 'sql query'
  
          Connect(mSOH) 'msoh is a connection string from My.Settings'
  
          Dim sqlCommand As New MySqlCommand
          With sqlCommand
              .Connection = connection
              .CommandText = sql
          End With
  
  
          Try
              Dim sqlReader As MySqlDataReader = sqlCommand.ExecuteReader()
              While sqlReader.Read()
                  Return True
              End While
  
          Catch ex As MySqlException
              Logi.LogInfo(ex)
          Catch ex As Exception
              Logi.LogInfo(ex)
          Finally
              Disconnect()
          End Try
  
          Return False
  End Function
  </code></pre>
  
  <p>And <code>Connect</code> and <code>Disconnect</code> methods if they are important:</p>
  
  <pre><code>    Private Sub Connect(shop As String)
  
              Select Case shop
                  Case "jablotron"
                      connection = New MySqlConnection(csJablotron)
                  Case "bcs"
                      connection = New MySqlConnection(csBCS)
                  Case "mSOH"
                      connection = New MySqlConnection(csmSOH)
                  Case Else
                      connection = New MySqlConnection(shop)
              End Select
  
              Try
                  connection.Open()
              Catch ex As MySqlException
                  Logi.LogInfo(ex)
              Catch ex As Exception
                  Logi.LogInfo(ex)
              End Try
          End Sub
  
  
          Private Sub Disconnect()
              Try
                  connection.Dispose()
              Catch ex As MySqlException
                  Logi.LogInfo(ex)
              Catch ex As Exception
                  Logi.LogInfo(ex)
              End Try
      End Sub
  </code></pre>
  
  <p>So if I try to check each row by this way it takes some time (around &lt;1 second if database is on localhost, and around 30 second if database is on remote server and I try to connect via VPN). Is this way being optimal - checking each row like this? Is it a good approach? Please give me some tip and advice :) I know that code is in VB.NET, but guys from C# can also help me :)</p>