closing a connection
-
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
-
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
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 theusing
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; }
-
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 theusing
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; }