In a “using” block is a SqlConnection closed on return or exception?

Either way, when the using block is exited (entier by successful completion or by error) it is closed.

Yes Yes. Either way, when the using block is exited (entier by successful completion or by error) it is closed. Although I think it would be better to organize like this because it's a lot easier to see what is going to happen, even for the new maintenance programmer who will support it later: using (SqlConnection connection = new SqlConnection(connectionString)) { int employeeID = findEmployeeID(); try { connection.Open(); SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection); command.

CommandType = CommandType. StoredProcedure; command.Parameters. Add(new SqlParameter("@EmployeeID", employeeID)); command.

CommandTimeout = 5; command.ExecuteNonQuery(); } catch (Exception) { /*Handle error*/ } }.

1 Although you almost never want to catch base-class Exception. Only catch those Exception descendants you know how to handle. – TrueWill Jan 17 at 21:06 @TrueWill - I agree.

I just moved the code around a bit for structure. – David Stratton Jan 17 at 21:10.

Yes to both questions. The using statement gets compiled into a try/finally block using (SqlConnection connection = new SqlConnection(connectionString)) { } is the same as SqlConnection connection = null; try { connection = new SqlConnection(connectionString); } finally { if(connection! = null) ((IDisposable)connection).Dispose(); } Edit: Fixing the cast to Disposable http://msdn.microsoft.com/en-us/library/yh598w02.aspx.

It isn't exactly that, but it is close enough. The exact difference isn't important. – Bryan Jan 17 at 21:15.

Dispose simply gets called when you leave the scope of using. The intention of "using" is to give developers a guaranteed way to make sure that resources get disposed. From MSDN: A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.

Using generates a try / finally around the object being allocated and calls Dispose() for you. It saves you the hassle of manually creating the try / finally block and calling Dispose().

In your first example, the C# compiler will actually translate the using statement to the following: SqlConnection connection = new SqlConnection(connectionString)); try { connection.Open(); string storedProc = "GetData"; SqlCommand command = new SqlCommand(storedProc, connection); command. CommandType = CommandType. StoredProcedure; command.Parameters.

Add(new SqlParameter("@EmployeeID", employeeID)); return (byte)command.ExecuteScalar(); } finally { connection.Dispose(); } Finally statements will always get called before a function returns and so the connection will be always closed/disposed. So, in your second example the code will be compiled to the following: try { try { connection.Open(); string storedProc = "GetData"; SqlCommand command = new SqlCommand(storedProc, connection); command. CommandType = CommandType.

StoredProcedure; command.Parameters. Add(new SqlParameter("@EmployeeID", employeeID)); return (byte)command.ExecuteScalar(); } finally { connection.Dispose(); } } catch (Exception) { } The exception will be caught in the finally statement and the connection closed. The exception will not be seen by the outer catch clause.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions