Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is it necessary to check a SqlConnection's state before opening or closing it? That is, is it necessary to do this:
C#
SqlConnection myConn = new SqlConnection(myConnString);
try
{
    SqlCommand myCmd = new SqlCommand(myCmdText, myConn);
    if (myConn.State != ConnectionState.Open)
        myConn.Open();
    myCmd.ExecuteNonQuery();
}
finally
{
    if (myConn.State != ConnectionState.Closed)
        myConn.Close();
}


or can I just call Open, Execute, Close? My boss thinks it's a best practice to check it before it is opened, I think it adds unnecessary code because it would be better to have only one Execute statement per try block.
Posted

Unless I am missing something obvious, no, you do not need to check the state because you just created it with new(). Now, if you were using a common class and were uncertain of the state, then yes it would be a good idea.
 
Share this answer
 
Comments
VJ Reddy 13-Apr-12 12:12pm    
Good answer. 5!
Hi,
You must open a connection before using it and you should close a connection after running a query. However, in the above code you do not need to check the status of the connection :
C#
if (myConn.State != ConnectionState.Open)

as you know it is closed, so just write:
C#
myConn.Open();


Cheers
 
Share this answer
 
Comments
VJ Reddy 13-Apr-12 12:12pm    
Good answer. 5!
Reza Ahmadi 13-Apr-12 12:19pm    
Thanks
I always create a new connection just before the try statement, but there might be places in our code where a connection is reused multiple times. That being said, checking the state before opening or closing it will (sometimes) prevent InvalidOperationException from throwing, so it could be useful for that reason.
 
Share this answer
 
Comments
VJ Reddy 13-Apr-12 12:28pm    
Good answer. 5!
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)[^]
 
Share this answer
 
v4
I agree with others that it is not necessary to check before opening connection. But, I suggest that it is mandatory to check connection state before closing connection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900