As
Reza Ahmadi said in Solution 2, it is necessary to open connection, before it is used. But
SqlConnection.Open
Method will throw
InvalidOperationException
if the connection is already open as explained here.
SqlConnection.Open Method[
^]
So if you are not sure about the current state of the connection, then I think it is better idea to check state to avoid throwing an exception.
As
ryanb31 said in Solution 1 for the situation given in the question, it is not necessary to check whether connection is open, as the connection is created just above.
But, I think using block is much elegant for the situation given in the question as given here
SqlConnection with using block (scroll down to see the using block)[
^]