Jack
Jack

Reputation: 7547

Can we cancel query issued to sql server?

I have a complex piece of SQL and it involves lot of calculations etc. I want to know whether it is possible to cancel the query that is issued to the SQL server?

Ex. There is a button named Search and user clicks on Search. I want to show a button named "Cancel" and that should cancel the query issued to the SQL server.

Is this possible?

Upvotes: 1

Views: 1070

Answers (1)

Uwe Keim
Uwe Keim

Reputation: 40736

There are a lot of asynchron functions in ADO.NET, e.g. SqlCommand.BeginExecuteNonQuery.

You can call these functions in your application, store the result object and cancel it when the user clicks the "Cancel" button.

So in pseudo-code you can do these steps:

  1. User clicks the Search button.
  2. In the button's handler, open your DB connection and the like.
  3. Show your Cancel button.
  4. Call the ADO.NET async function of your choice.
  5. Store the IAsyncResult returned object of the function call (for detecting when the operation has finished to hide the cancel button again).
  6. In the cancel button's handler, call the Cancel method of SqlCommand.

Upvotes: 8

Related Questions