我正在使用Linq to SQL,并在博客文章中读到有关尽快关闭数据库连接的内容。例如,他们展示了将变量转换为列表(使用.ToList())而不是实际返回Linq查询的示例。我有以下代码:
public static bool HasPassword(string userId)
{
ProjDataContext db = new ProjDataContext();
bool hasPassword = (from p in db.tblSpecUser
where p.UserID == userId
select p.HasPassword).FirstOrDefault();
return hasPassword;
}
这个查询是否合适?还是数据库连接会比必要的时间更长地保持打开状态?
感谢任何建议。
provider
成员,并将services
、tables
和loadOptions
成员设置为null。在SqlProvider
中的Dispose方法(由DataContext Dispose方法调用)会关闭connection
成员,并将其他一些成员设置为null。因此,我不会说它什么都没做。 - Robert Harvey