Open SQLConnection when Database Stopped

Last post 10-30-2008 7:01 AM by kapil.dalvi. 6 replies.

Sort Posts:

  • Open SQLConnection when Database Stopped

    12-08-2003, 1:43 PM
    • Member
      140 point Member
    • damich
    • Member since 10-23-2002, 11:16 AM
    • Posts 33
    I'm hoping someone can explain to me exactly what the SQLConnection.Open() method does, especially when the database is stopped. I'm trying to include some error processing in my program, specifically to ensure the database is up and running. But even with the database stopped, the Open() statement works fine. Later, when I try to read from the database, I get the error. I'd like to stop it before it gets any further. Why is the Open() statement "working" even with the database stopped?
  • Re: Open SQLConnection when Database Stopped

    12-08-2003, 1:57 PM
    • Star
      14,520 point Star
    • donkiely
    • Member since 06-18-2002, 6:28 PM
    • Fairbanks, Alaska
    • Posts 2,445
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    Hmm. Interesting about the Open. Are you sure you aren't swallowing the exception that might arise? I've gotten exceptions when the db wasn't running. Why don't you post your code so we can see if that might be the problem?

    If you want to be sure that the connection is open--which is good practice since connections can be lost--you can use the SqlConnection object's State property. It returns a ConnectionState Enumeration value.

    Don
    Don Kiely, MCP, MCSD
    In the Last Frontier, Interior Alaska
    Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!
  • Re: Open SQLConnection when Database Stopped

    12-08-2003, 2:05 PM
    • Member
      140 point Member
    • damich
    • Member since 10-23-2002, 11:16 AM
    • Posts 33
    I'm following through the code with debug, so I can see the Open() work successfully, then see the error occur on the Read(). But I noticed something else...it only works the first time after the database has been stopped, and I think it may be using a pool connection that has remained open. The next time an Open() is attempted, it does give me an error. So closing a database is not disposing of any open connections, it would appear. Does this sound correct? If so, is there any way around it?
  • Re: Open SQLConnection when Database Stopped

    12-08-2003, 2:42 PM
    • Star
      14,520 point Star
    • donkiely
    • Member since 06-18-2002, 6:28 PM
    • Fairbanks, Alaska
    • Posts 2,445
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    Ah. That could indeed be the problem. What happens when you check the connection's state before you use it, but after the database stops? It should give you something other than open.

    OTOH, do you stop your db often?

    Incidentally, you could capture the SqlException you're getting and do something in response, such as notify the user, wait a few seconds and try again, whatever makes sense for the application.

    Don
    Don Kiely, MCP, MCSD
    In the Last Frontier, Interior Alaska
    Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!
  • Re: Open SQLConnection when Database Stopped

    12-08-2003, 4:08 PM
    • Member
      140 point Member
    • damich
    • Member since 10-23-2002, 11:16 AM
    • Posts 33
    The connection's state shows Open after I've successfully opened it once, then closed the db. It continues to show Open until the next db operation fails (e.g., Read), then it changes to Closed. So testing for it won't work either. I think your second idea is best, just respond to the type of SQLException that is raised and don't try to do too much with the Open condition.

    No, we don't purposely stop the db, but I want to be able to exit the application on our terms regardless of the problem.

    Thanks for your help and comments!
  • Re: Open SQLConnection when Database Stopped

    12-08-2003, 4:20 PM
    • Star
      14,520 point Star
    • donkiely
    • Member since 06-18-2002, 6:28 PM
    • Fairbanks, Alaska
    • Posts 2,445
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    I want to be able to exit the application on our terms regardless of the problem.

    Quite wise.

    Take care,
    Don
    Don Kiely, MCP, MCSD
    In the Last Frontier, Interior Alaska
    Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!
  • Re: Open SQLConnection when Database Stopped

    10-30-2008, 7:01 AM
    • Member
      2 point Member
    • kapil.dalvi
    • Member since 10-30-2008, 6:49 AM
    • Posts 1

    Hi,

    I have the same problem mentioned by you. I have Client n server Setup both on different machine, When I disconnect the Client on my machine from network whn the application is running, for First time it finds the Connection OPEN and further tries to fetch the data from DB n shows the TCP network error, while again if i trie using the appln further it gives create connection error.

    Why is it that for first time it finds or rather get the connection open. Since if the network is disconnected the connection object should not open even for first time.

     Please let me know if you have any soltuion for this, since on netwrk failure i will be making n appln exit, now both time the exception comes from SQL exception and other sql exception thru SP also comes under SQL exception having same ERROR CODE, so not able distinguish which one is network failure and hence can't give user friendly error msg and make appln exit.

     Thanks in advance,

    Kapil

Page 1 of 1 (7 items)