HOW USE THE DISPOSE METHOD FOR CLOSE THE CONNECTION

Rajesh Goswami

Total Post:29

Points:203
Posted by  Rajesh Goswami
 2096  View(s)
Ratings:
Rate this:
explain it
  1. yash Singh

    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. Shankar M

    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 Feb-25-2013 09:18:25 PM
  1. Shankar M

    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

  1. Shankar M

    Post:25

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

    Post is removed by the User.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!