We always use SqlConnection class for making connections
1 Simple SqlConnection
SqlConnection=new SqlConnection()
If any exception occurs inside while block it throws exception the connection.close() process will not executed due to the exception.To avoid this situation, we can take the help of the try....catch... finally block by closing the connection inside the finally block or inside the catch block.
2 using SqlConnection
using (SqlConnection connection = new SqlConnection(connectionString))
{
}
Using keyword automatically destroys the database connection when not needed. It also invoke the connection when required for making connections
The difference between the two is while hosting the code in IIS using will reduce the effort on Sql Server by automatically closing and opening connections .If in any case error occurs the without "using" keyword will go to situation where database resources gets engaged unnecessarily
0 Comment(s)