Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. terminating SqlConnection.Open() call while connecting

terminating SqlConnection.Open() call while connecting

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    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.

    L 2 Replies Last reply
    0
    • D Danzy83

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • D Danzy83

        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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Use OpenAsync[^], or it's overload[^]. --edit For which version of Sql Server? Take note of the "supported platforms" in the documentation.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        D 1 Reply Last reply
        0
        • L Lost User

          Use OpenAsync[^], or it's overload[^]. --edit For which version of Sql Server? Take note of the "supported platforms" in the documentation.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          D Offline
          D Offline
          Danzy83
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • D Danzy83

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups