我有这个通常返回 20 000 - 100 000 行数据的大 SQL 命令。但是一旦我调用 executeMyQuery 函数,程序就会挂起几秒钟,具体取决于返回的大小。
我只返回一列。
如何在此命令运行时显示进度条?
也许在线程或其他东西中(我没有线程经验)
这是我的代码(参数是从 3 个不同的 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