Search code examples
.netdatabase-connectionsqlclientreconnect

Can be closed or broken SqlConnection reconnected simply by calling Open()?


In my method, if I find that connection (System.Data.SqlClient.SqlConnection) which I received has

  • State = Closed
    or
  • State = Broken

which of the following approaches is correct?

  1. Call Open() to reconnect and continue with execution of subsequent statements. (Of course, I can also give the Open() pre-configured number of retries.)

  2. Throw such a connection away as unrecoverable and get brand new one.

(If these approaches are equal, I would prefer the first as easier to implement.)


Solution

  • (ConnectionState.Broken) The connection to the data source is broken. This can occur only after the connection has been opened. A connection in this state may be closed and then re-opened. (This value is reserved for future versions of the product.)

    i.e. you can close and re-open the connection with Open(). If State is closed than you should throw an exception