在SQL Server 2005中,是否有方法从.NET应用程序中指定多个连接字符串,其中一个是主要首选连接,但如果不可用,则默认尝试其他连接(这可能会去比较DB /服务器等)?SQL Server 2005连接问题
如果没有沿着这些确切的线,有什么我们可以使用,而不诉诸编写某种循环代码来检查连接?
谢谢。
在SQL Server 2005中,是否有方法从.NET应用程序中指定多个连接字符串,其中一个是主要首选连接,但如果不可用,则默认尝试其他连接(这可能会去比较DB /服务器等)?SQL Server 2005连接问题
如果没有沿着这些确切的线,有什么我们可以使用,而不诉诸编写某种循环代码来检查连接?
谢谢。
不幸的是,没有这样做的FCL方法 - 你需要自己实现这个。
我们通常会在我们的SqlConnection对象上使用组合来检查这一点。所有的数据访问都是通过后端类来完成的,我们在web/app.config中指定了多个服务器。 (请原谅任何错误,我其实用手写了这一点)
这将是这个样子:
class MyComponent
{
private SqlConnection connection;
....
public void CheckServers()
{
// Cycle through servers in configuration files, finding one that is usable
// When one is found assign the connection string to the SqlConnection
// a simple but resource intensive way of checking for connectivity, is by attempting to run
// a small query and checking the return value
}
public void Open()
{
connection.Open();
}
public ConnectionState State
{
get {return connection.State;}
set {connection.State = value;}
}
// Use this method to return the selected connection string
public string SelectedConnectionString
{
get { return connection.ConnectionString; }
}
//and so on
}
这个例子不包括错误检查或错误日志,确保你补充的是,这样的对象可以选择报告哪些连接失败以及为什么。
假设你想访问同一组数据,那么你可以使用集群或镜像来提供高可用性。
SQLNCLI provider支持SQL Server数据库镜像
Provider=SQLNCLI;Data Source=myServer;Failover Partner=myMirrorServer
集群只使用虚拟SQL实例名称。
否则,我不太懂为什么你要做到这一点...