so as with all things that have unmanaged resources inside them ( which the db connection is )
then you need to control it's lifetime.
the easiest first step is to use the dispose pattern
using (MyDataContext context = new MyDataContext())
{
... do some Linq type thing
}
or perhaps..
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
Using ( SqlCommand cmd = new SqlCommand("sproc_GetThingCount", cn) )
{
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
return (int)cmd.ExecuteScalar();
}
}
however with LINQ you can have deferred loading going on which might cause you issues
so you might want to solve that by
using (MyDataContext context = new MyDataContext())
{
context.DeferredLoadingEnabled = false;
...
}
then of course you could hang on to one context and share in someway...