在下面的代码示例中,当"ListOfLists"方法完成后,我的数据上下文连接是否会保持打开状态?我需要显式关闭它吗,还是它会保持打开状态并可供其他方法使用。
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;
}
}
catch(Exception e)
。第三,除非您实际上要处理异常的原因并修复它,或者尝试再次执行它,否则应该throw
异常,以便在调用堆栈中进一步处理它。 - Stuart Grassie