Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - interrupt execution of stored procedure

I am working on a C# application that calls stored procedures from a SQL server. Depending on the search parameters the user inputs, the stored procedure can take a while and return many records.

I want to implement a cancel button so the user can cancel the execution of the stored procedure when it takes to long. I run the call to the stored procedure in a new thread so the GUI doesn't get affected when the stored procedure takes a while. I read about aborting a thread but found many rejecting opinions about using abort.

Possible solutions i think of:

  • Is there a common manner to stop executing the stored procedure (which is a method in the c# code)
  • Is there a better way to stop the thread ? (I found also Thread.Interrupt() but this only works on a blocked thread, not on running code)
  • I can abandon the thread and start a new one but then unnecessary database and network resources are used
  • Is there a way to stop the stored procedure from the server side ?
like image 643
svh Avatar asked Apr 01 '11 11:04

svh


People also ask

How do you stop a stored procedure from running?

To disable a stored procedure permanently, you can: Drop the procedure using the DROP PROCEDURE statement. Use an ALTER PROCEDURE statement. Rename or delete the z/OS load module.

Is LINQ faster than stored procedures?

Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure.

Can we use stored procedure in LINQ?

Language-Integrated Query (LINQ) makes it easy to access database information, including database objects such as stored procedures. The following example shows how to create an application that calls a stored procedure in a SQL Server database.


1 Answers

You might not want to use Linq here. Try this:

Stop SQL query execution from .net Code

Basically use SqlCommand.Cancel()

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

like image 188
Ryan Bennett Avatar answered Oct 02 '22 23:10

Ryan Bennett