My project must connect to a Sql Server quite often to read/write datas.
I am wondering : do i have to open the connection to the Sql Server at
the beginning when the application loads, then leave the connection open
and close it when the application closes ? Or is it more efficient
toopen and close the connection each time i have to use a datareader, a
command etc...
Hello Marine,
Create the connection object and close the connection after every use.
The Connections to Sql Server are pooled anyways, so there is little
overhead creating new oonnection objects.
Always use the 'using' keyword to ensure you really close/dispose your
connections like so :
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
command.Execute();
}
If you don't, you might end up leaking connections.
Ben