In the code sample below, will my data context connection stay open once the ListOfLists method is completed? Do I need to explicitly close it, or will it stay open and be available for other methods.
public static Dictionary<int, string > ListOfLists()
{
try
{
ListDataDataContext db = new ListDataDataContext(GetConnectionString("Master"));
return db.ListMatchingHeaders
.Select(r => new { r.ListRecNum, r.ListName })
.ToDictionary(t => t.ListRecNum, t => t.ListName);
}
catch (Exception)
{
MessageBox.Show("Could not connect to database, please check connection and try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
It’s still open. You will need to explicitly dispose/close of the connection or it’s possible that you might have memory or connection pool problems. I recommend that you wrap your context around a
usingblock.