I have this big SQL command that usually returns 20 000 - 100 000 rows of data. But as soon as i call the executeMyQuery function, the program hangs for a few seconds depending on how large the return is.
I only return one column.
How can I display a progress bar while this command is running?
Maybe in a Thread or something(I have NO experience with threads)
Here is my code(The arguments are sent from 3 different combobox.selectedItem) :
Public Function executeMyQuery(dbname As String, colname As String, tblname As String)
Try
ListBox1.Items.Clear()
If Not String.IsNullOrWhiteSpace(connString) Then
Using cn As SqlConnection = New SqlConnection(connString)
cn.Open()
Using cmd As SqlCommand = New SqlCommand()
cmd.Connection = cn
Dim qry As String
qry = String.Format("select distinct [{0}] from {1}.dbo.{2} where [{0}] is not null", colname, dbname, tblname)
cmd.CommandText = qry
cmd.CommandTimeout = 0
Dim count As Integer
Using myReader As SqlDataReader = cmd.ExecuteReader()
While (myReader.Read())
count += 1
ListBox1.Items.Add(count.ToString & ". " & myReader.GetString(0))
End While
End Using
End Using
End Using
End If
cn.Close()
Catch ex As Exception
MsgBox("Error Occured : " & ex.Message)
cn.Close()
End
End Function
Readerwill be returned with the first row, you'll need to increment a progress bar as you enumerate the rows by callingmyReader.Read. The SQL part is already asycnronous. However, if you want the changes to the progress bar to be visible to the user while the data is read you'll need to runexecuteMyQueryin a background thread to leave the main GUI thread free to update the progress bar as you trigger the change events by incrementing the progress.