Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. closing a connection

closing a connection

Scheduled Pinned Locked Moved ASP.NET
performance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    karanba
    wrote on last edited by
    #1

    Hi, I want to learn, which will be is true for best performance. Are those the same thing that not to close a connection and leave it to dispose when the scope finishes. // do not close connection public static SqlDataAdapter getDataAdapter(SqlCommand cmd, string connectionString) { SqlConnection con = new SqlConnection(connectionString); cmd.Connection = con; con.Open(); return new SqlDataAdapter(cmd); } or // close connection public static SqlDataAdapter dataAdapter(SqlCommand cmd, string connectionString) { SqlConnection con = new SqlConnection(connectionString); cmd.Connection = con; con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd) con.Close(); return da; }

    karanba

    G 1 Reply Last reply
    0
    • K karanba

      Hi, I want to learn, which will be is true for best performance. Are those the same thing that not to close a connection and leave it to dispose when the scope finishes. // do not close connection public static SqlDataAdapter getDataAdapter(SqlCommand cmd, string connectionString) { SqlConnection con = new SqlConnection(connectionString); cmd.Connection = con; con.Open(); return new SqlDataAdapter(cmd); } or // close connection public static SqlDataAdapter dataAdapter(SqlCommand cmd, string connectionString) { SqlConnection con = new SqlConnection(connectionString); cmd.Connection = con; con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd) con.Close(); return da; }

      karanba

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You should definitely close the connection. If you don't, the connection object will be lying around in memory, still connected, until the garbage collector is forced to finalize it. If you run out of available connections before you run out of memory (which will cause a garbage collection), the server will be unable to connect to the database at all until some of the open connections eventually time out. Objects in .NET are not finalized when they go out of scope, they are just left to be garbage collected. That's why there is a Dispose method in the first place. It's used to tell objects that you are done with them, so that they can release all unmanaged resources. To make sure that the connection is always closed properly, you should use a using block. When you leave the using block, the connection is always disposed (which also closes it).

      public static SqlDataAdapter dataAdapter(SqlCommand cmd, string connectionString) {
      SqlDataAdapter da;

      using (SqlConnection con = new SqlConnection(connectionString)) {
      	cmd.Connection = con;
      	con.Open();
      	da = new SqlDataAdapter(cmd)
      }
      return da;
      

      }

      --- b { font-weight: normal; }

      K 1 Reply Last reply
      0
      • G Guffa

        You should definitely close the connection. If you don't, the connection object will be lying around in memory, still connected, until the garbage collector is forced to finalize it. If you run out of available connections before you run out of memory (which will cause a garbage collection), the server will be unable to connect to the database at all until some of the open connections eventually time out. Objects in .NET are not finalized when they go out of scope, they are just left to be garbage collected. That's why there is a Dispose method in the first place. It's used to tell objects that you are done with them, so that they can release all unmanaged resources. To make sure that the connection is always closed properly, you should use a using block. When you leave the using block, the connection is always disposed (which also closes it).

        public static SqlDataAdapter dataAdapter(SqlCommand cmd, string connectionString) {
        SqlDataAdapter da;

        using (SqlConnection con = new SqlConnection(connectionString)) {
        	cmd.Connection = con;
        	con.Open();
        	da = new SqlDataAdapter(cmd)
        }
        return da;
        

        }

        --- b { font-weight: normal; }

        K Offline
        K Offline
        karanba
        wrote on last edited by
        #3

        thanks a lot a get a clear answer.

        karanba

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups