none
Cancel SQLConnection while Connecting RRS feed

  • Question

  • Is there a way to cancel a SQLConnection object during an Open reliably?  I've tried putting it in a thread and Aborting the thread works as long as the server name is not resolvable.

     

    For instance, if I use Blue as the name of the SQL Instance, I can Abort the thread, but if I use google.com as the name of the SQL Instance, when I abort the thread it hangs.

     

    Alternatively is there a way to verify a server name and then verify a sql instance?

     

    Thanks

    Monday, January 28, 2008 5:31 PM

Answers

  • Hi Lary

     

    At present it is not possible to cancel SqlConnection.Open method call. Also, it is not possible to verify a Sql Server instance without connecting to it.

     

    I think using a different thread for creating the connection is the best option for your scenario. In the case when the user cancels the operation you should let the Open call finish, and once it has finshed dispose the SqlConnection object and then exit the thread.

     

    Thanks

    Himanshu

    Tuesday, January 29, 2008 8:28 AM

All replies

  • "Alternatively is there a way to verify a server name and then verify a sql instance?"

     

    I'd suggest using the SqlDataSourceEnumerator if the servers you're trying to connect to are within your LAN rather than across the internet. This way you'll have an accurate list of available servers. It may not show ALL of the servers as it works somewhat like polling for available wireless networks, especially if one or more are particularly busy and don't respond to the UDP broadcast before the timeout period, but any that do show up are atleast valid. Running the enumerator multiple times may yield different results.

    Monday, January 28, 2008 6:31 PM
  •  

    I don't think that will work for me.  The SQL Server may or may not be on the local internet and generally won't be on the same subnet. 

    I believe I need a way to either attempt to make a connection and be able to cancel it, or have a way to check each of the pieces of a connection string to validate them before attempting to connect.  If I validate the pieces then each check needs to be cancelable also.

     

    What I'm trying to accomplish is: Display a connecting progress bar with a cancel button.  If the user presses the cancel button, the connection attempt is canceled and the user moves on.

    Monday, January 28, 2008 6:51 PM
  • Hi Lary

     

    At present it is not possible to cancel SqlConnection.Open method call. Also, it is not possible to verify a Sql Server instance without connecting to it.

     

    I think using a different thread for creating the connection is the best option for your scenario. In the case when the user cancels the operation you should let the Open call finish, and once it has finshed dispose the SqlConnection object and then exit the thread.

     

    Thanks

    Himanshu

    Tuesday, January 29, 2008 8:28 AM
  • There is no way you can cancel the Open method call in between before the timeout period is reached. So you can use ConnectionTimeout property to set appropriate timeout value.

    Hope this helps.
    Tuesday, January 29, 2008 10:33 AM
  •  

    Turns out this is actually easy, I was just doing too much work...

     

    What I ended up doing was creating a class that tests the connection.

     

    That class I start in a thread.  When the user presses cancel I just abandon the thread.  It will finish erroring out in 30 seconds or so with no ill effects to the application.  Attempting to abort the thread is what was hanging the application.  The application is nice and responsive now and does exactly what I want.  The only price is that in the background there may be one or two threads working on getting through their errors.

     

    Thanks for the replies.

    Tuesday, January 29, 2008 4:44 PM