Reputation: 53
In SQL we open connection
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
or
using (conn = new SqlConnection(connectionString))
{
cmd1.Connection = conn;
conn.Open();
cmd1.ExecuteNonQuery();
}
What is best practice for LINQ to SQL:
DataClassesLinqDataContext dbLinq = new DataClassesLinqDataContext();
or
using(DataClassesLinqDataContext dbLinq = new DataClassesLinqDataContext())
{
var x = ...
}
Upvotes: 2
Views: 183
Reputation: 147374
Yes, best practise is to dispose of the context to free up the resources it holds so I'd go with the using ()
Upvotes: 4
Reputation: 2569
offcourse with using "using" clause, it just ensures the dispose method gets called once it ends its scope. No need to free expensive variables like database connection.
Upvotes: 2