Frosty840
Frosty840

Reputation: 8335

Interrupting DataAdapters?

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?

Upvotes: 1

Views: 133

Answers (1)

Brian Gideon
Brian Gideon

Reputation: 48949

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 Close on the underlying SqlConnection instance. The documentation says this will terminate and rollback any pending transactions. Also, instead of using a SqlDataAdapter (which unfortunately does not have the async Fill methods) you could use SqlCommand.BeginExecuteReader and once that is done you can fill a DataTable by calling the overload of DataTable.Load that accepts an IDataReader. If you want to interrupt a pending a command just call Close.

Upvotes: 1

Related Questions