10

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?

1
  • Can you Add filters in your Stored Procedure? Try to read more about sql best practices.. Commented Jan 24, 2011 at 7:48

3 Answers 3

17

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

Sign up to request clarification or add additional context in comments.

1 Comment

Apparently closing the command is more likely to work 100% of the time: stackoverflow.com/questions/889102/…
11

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.

2 Comments

the command must be running in another thread ? real world sample ?
FWIW, setting the CommandTimeout doesn't cancel the query on the database server for MySQL. It just ends the waiting in the client. This needs to be called explicitly @ MySql Connector/Connection.cs.
1

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. :) :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.