Handling Different ConnectionStates Before Opening SqlConnection

If you need to open SqlConnection before issuing queries, can you just handle all non-Open ConnectionStates in the same way? For example:

    if (connection.State != ConnectionState.Open)
    {
        connection.Open();
    }

I read somewhere that for ConnectionState.Broken you need to close the connection before reopening it. Does anyone have any experience? Thanks -

+5
source share
3 answers

http://msdn.microsoft.com/en-us/library/system.data.connectionstate.aspx

The broken connection state must be closed and reopened before it is allowed for future use.

: , . ConnectionState, . , switch .

+5

, . ADO.NET , . (, ASP.NET), WinForms, .

:

using(SqlConnection connection = new SqlConnection(...))
{
   connection.Open();
   // ... do your stuff here

}  // Connection is disposed and closed here, even if an exception is thrown

.

+4

You can handle it the same way. I got a lot of == connection states broken when using IE9. IE7 has something fundamentally wrong, since no other browser has this problem with broken connection states after 5 or 6 database table updates. Therefore, I use the context of the object. So just close it and open it again.

I have this code before all my readings and updates in the businss logical level:

if (context.Connection.State == System.Data.ConnectionState.Broken)
{
    context.Connection.Close();
    context.Connection.Open();
}
+3
source

All Articles