how use the dispose method for close the connection

Total Post:29

Points:203
 2454  View(s)
Ratings:
Rate this:
explain it
  1. Post:15

    Points:105
    Re: how use the dispose method for close the connection

    Dispose() method permanently removes connection object from memory and the resource no longer exists for any further          processing.


    string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
    SqlConnection sqlcon = new SqlConnection(constring);
     sqlcon.Open();   // connection is open

    try
    {
    //  code here which will be execute
    }
    catch
    {
         // code  will be execute when  error occurred in try block
    }
    finally
    {
     sqlcon.Dispose();         // close and desroy the  connection object
    }

      Modified On Dec-18-2010 06:28:01 AM
  1. Post:25

    Points:175
    Re: how use the dispose method for close the connection

    Hi Rajesh,


    You don't need to explicitly close the connection that is opened, You can use the Using Clause.

    using(SqlConnection sqlcon = new SqlConnection(constring))
      {
       }

    The leverage of using it is the resources tied up to it are freed automatically with out explicitly calling the
    Dispose() Method.

    Thanks,
    Shankar

      Modified On Mar-29-2018 10:55:18 PM
  1. Post:25

    Points:175
    Re: how use the dispose method for close the connection

    Hi Rajesh,


    You don't need to explicitly close the connection that is opened, You can use the Using Clause.

    using(SqlConnection sqlcon = new SqlConnection(constring))
      {
       }

    The leverage of using it is the resources tied up to it are freed automatically with out explicitly calling the
    Dispose() Method.

    Thanks,
    Shankar

      Modified On Mar-29-2018 10:55:38 PM
  1. Post:25

    Points:175
    Re: how use the dispose method for close the connection

    Post is removed by the User.

Answer