terminating SqlConnection.Open() call while connecting
-
Hi everyone. I'm trying open a connection to sql server database in a background thread while showing a progress dialogbox that has a Cancel button. I would like to know how I can terminate the connection when the Cancel button is clicked and SqlConnection.Open call is still connecting. Thanks.
-
Hi everyone. I'm trying open a connection to sql server database in a background thread while showing a progress dialogbox that has a Cancel button. I would like to know how I can terminate the connection when the Cancel button is clicked and SqlConnection.Open call is still connecting. Thanks.
I'm afraid you cannot do that. SqlConnection.Open is a blocking call and control will not return to the calling code until the method completes. And I'm not aware of any asynchronous way of opening a database connection. I stand corrected. Check Eddy's answer below for an asynchronous way of doing this. :-)
-
Hi everyone. I'm trying open a connection to sql server database in a background thread while showing a progress dialogbox that has a Cancel button. I would like to know how I can terminate the connection when the Cancel button is clicked and SqlConnection.Open call is still connecting. Thanks.
-
Thanks Eddy. The function is available only in .net framework 4.5 which I'm not using. I tried in my own way which works fine now. My BackgroundWorker thread creates a new thread and the created thread rather calls SqlConnection.Open(). When the cancel button is clicked, the BackgroundWorker thread aborts the thread opening the connection. It works fine although I don't know if there could be any related problems. I have run the application several times just to try that part of the code and I haven't found any problems.
-
Thanks Eddy. The function is available only in .net framework 4.5 which I'm not using. I tried in my own way which works fine now. My BackgroundWorker thread creates a new thread and the created thread rather calls SqlConnection.Open(). When the cancel button is clicked, the BackgroundWorker thread aborts the thread opening the connection. It works fine although I don't know if there could be any related problems. I have run the application several times just to try that part of the code and I haven't found any problems.
Dan_K wrote:
I tried in my own way which works fine now. My BackgroundWorker thread creates a new thread and the created thread rather calls SqlConnection.Open(). When the cancel button is clicked, the BackgroundWorker thread aborts the thread opening the connection.
That's what the OpenAsync in 4.5 will do; open the query on a backgroundthread, polling it's status.
Dan_K wrote:
It works fine although I don't know if there could be any related problems.
Opening a connection can fail for multiple reasons, and exceptions aren't casted across the thread. You might want to make sure you have a good exception-handler in there; if you have the time, then it'd be mighty cool to have a "try connecting again" function - that way people won't have to relaunch the app after they found out that the database-server wasn't powered on.
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]