Stop SQL query execution from .net Code

I’m executing one stored procedure from the ‘.net’ code. Since there is a lot of data, it is taking too much time to execute. Is there any way to stop this execution from the c# code?

In other words, if we execute the query from database itself, there is a option to stop its execution but in the code is it possible?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes sqlcommand.cancel is your friend http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.cancel.aspx

Method 2

Yes, you can cancel the query by using SqlCommand.Cancel. However, the command must be running in another thread (otherwise Cancel will be called only after the command is finished).

Also you can specify a CommandTimeout if you want to cancel this query when it runs over a specified time limit.

Method 3

You can do myCommand.Cancel(), and this will work best as even i have face similar issue in past, and using myCommand.Cancel() i have solved my issue. 🙂 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x