Correct way to do a DbConnection.TryOpen() ? RRS feed

  • Question


    I would like to know what is the "correct" way of checking for the MS SQL Server connectivity (given the proper connection string)?


    Currently, I am using a Try / Catch with a SqlConnection.Open.


    I am doing this to display a custom error page when the DB connectivity is lost for a ASP.NET web server (because obviously, the error isn't going to be logged, precisely because there is no database connectivity).


    Thanks in advance,

    Joannès sales forecasting

    Wednesday, March 19, 2008 8:25 AM


  • I'm not sure I understand you. Do you call SqlConnection.Open before every query?


    You can't verify SQL Server availability without a connection attempt. So you must call SqlConnection.Open and catch the exception. But do it only once you want to establish the connection.


    Besides the connection can be lost during the session so you should also wrap your queries with try/catch


    Hope this helps

    Wednesday, March 19, 2008 8:44 PM