How can I cancel a database query in ASP.NET when the user browser is disconnected?

ASP.NET (3.5) allows users to run complex queries, which can take up to 4 minutes, to return results.

When I make long loops of code, I sometimes check Response.IsClientConnected() to end the page if the user closes his browser or clicks the stop button.

But when I query SQL Server, my .NET code blocks when I call GetDataReader() .

Is there an easy way to do GetDataReader () asynchronously, so I can wait, but still check, say, every 5-10 seconds to find out if the user is all connected and instruct the database request, ve left?

Or is there another alternative that I don't think about?

+6
asynchronous sql-server
source share
2 answers

you can use sqlcommand BeginExecuteReader to get asynch sqlreader example

not sure 100% if you need?

cancel link

+5
source share

I would suggest creating an object with Start() and Wait() methods that encapsulate your request. Internally use ManualResetEvent to wait for the request to complete. You will use the Set() function when the request is completed, and the WaitOne() or WaitOne(TimeSpan ts) method to wait for it in your Wait() method.

+1
source share

All Articles