Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Using keyword while making connections

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 75
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: