我需要验证DataContext对象上的ConnectionString属性,以确保LINQ可以获得与数据库的连接。我已经尝试了下面两种方法,但是,如果连接字符串无效,它们会锁定应用程序。 LINQ有没有另外一种方法来做到这一点?如何测试Linq-to-SQL连接字符串以确保其有效
public static bool TestDBConnection(connectionString)
{
bool result = true;
DomainClassesDataContext db = new DomainClassesDataContext(connectionString);
try
{
// Hangs if connectionString is invalid rather than throw an exception
db.Connection.Open();
// Initially, I was just trying to call DatabaseExists but, this hangs as well if the conn string is invalid
if (!db.DatabaseExists())
{
result = false;
}
}
catch (Exception ex)
{
result = false;
logger.Fatal(ex.Message);
logger.Fatal(ex.StackTrace);
}
finally
{
db.Dispose();
}
return result;
}
它真的是_hang_还是只是倒计时的连接超时? –
啊,可能就是这样。我现在将检查... – Grasshopper
超时设置为0,但是,我无法弄清楚如何更改它,因为该属性上只有一个setter。 – Grasshopper