I have a circumstance where I’m popping up a dialog window containing a “Please Wait” message, running an SQL query, then replacing the “Please Wait” message with a DataGridView containing the collected results.
The code I’m using for this is basically:
Dim X As New Data.SqlClient.SqlCommand
X.CommandText = "SELECT some_data FROM someTable"
Dim XAdapter As New System.Data.SqlClient.SqlDataAdapter(X)
Dim XSet As New DataSet
XAdapter.Fill(XSet, "MyTable")
Me.someDataGridView.DataSource = XSet.Tables("MyTable").DefaultView
I would like to put the retrieval of this data into a background thread, so that the UI doesn’t freeze up while I’m loading larger datasets, but, having done this, I’d also like to allow the user to cancel and close the dialog, if accessing the data takes longer than they want to wait.
At the moment, this Dialog window can take some arbitrary SQL string and display the results (using SQLDataAdapter) without any extra code.
How can I make this process interruptible, while also retaining the ease of access of the SQLDataAdapter?
According to the documentation you should be able to call
Closeon the underlyingSqlConnectioninstance. The documentation says this will terminate and rollback any pending transactions. Also, instead of using aSqlDataAdapter(which unfortunately does not have the asyncFillmethods) you could useSqlCommand.BeginExecuteReaderand once that is done you can fill aDataTableby calling the overload ofDataTable.Loadthat accepts anIDataReader. If you want to interrupt a pending a command just callClose.