code_junkie

Handling different ConnectionStates before opening SqlConnection

Nov 11th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.27 KB | None | 0 0
  1. if (connection.State != ConnectionState.Open)
  2. {
  3. connection.Open();
  4. }
  5.  
  6. using(SqlConnection connection = new SqlConnection(...))
  7. {
  8. connection.Open();
  9. // ... do your stuff here
  10.  
  11. } // Connection is disposed and closed here, even if an exception is thrown
Add Comment
Please, Sign In to add comment