I am trying to design an efficient application that connects to a SQL Database and I was wondering what the merits/demerits of creating a SQL connection for each SQL query is, Eg, Like the code below, making a class with the connection set once so you can call on it when ever you need it in the class.
class SqlQuery
{
SqlConnection Connection = new SqlConnection(@myConnectionString);
public void fillInfoData()
{
SqlCommand updateCommand = new SqlCommand(
@"my sql query", Connection);
Connection.Open();
updateCommand.ExecuteNonQuery();
Connection.Close();
}
}
Or should you create a new connection for each new query?
- EDIT: Jon Skeet recommended creating a new connection for each query, I was wondering why?
- EDIT No 2: He stated it in this answer.