如何在VB.NET中将故障转移伙伴添加到连接字符串
我有一个windows应用程序连接到数据库来读取一些数据。由于数据库是为弹性而设置的,因此我的应用程序需要连接到两个数据库中的一个。是否可以指定使用sql server身份验证在连接字符串中指定故障转移伙伴的语法 非常感谢您的帮助。检查: 数据库镜像如何在VB.NET中将故障转移伙伴添加到连接字符串,.net,asp.net,sql-server,vb.net,connection-string,.net,Asp.net,Sql Server,Vb.net,Connection String,我有一个windows应用程序连接到数据库来读取一些数据。由于数据库是为弹性而设置的,因此我的应用程序需要连接到两个数据库中的一个。是否可以指定使用sql server身份验证在连接字符串中指定故障转移伙伴的语法 非常感谢您的帮助。检查: 数据库镜像 如果使用ADO.NET或SQL本机客户端连接到正在镜像的数据库,则应用程序可以利用驱动程序的功能在发生数据库镜像故障转移时自动重定向连接。必须在连接字符串和故障转移伙伴服务器中指定初始主体服务器和数据库 当然,还有许多其他方法可以使用数据库镜像来编
如果使用ADO.NET或SQL本机客户端连接到正在镜像的数据库,则应用程序可以利用驱动程序的功能在发生数据库镜像故障转移时自动重定向连接。必须在连接字符串和故障转移伙伴服务器中指定初始主体服务器和数据库 当然,还有许多其他方法可以使用数据库镜像来编写连接字符串,这只是指出故障切换功能的一个示例。您可以将其与其他可用的连接字符串选项结合使用 如果提供故障转移的名称 连接中的伙伴服务器 字符串,客户端将透明地 尝试与故障转移连接 如果主体数据库为 客户端启动时不可用 应用程序首先连接 如果忽略故障转移的名称 合作伙伴服务器和主体 数据库在以下情况下不可用: 客户端应用程序首先连接,然后 引发了SqlException
如果您没有在SQL Server之间设置镜像,您可以使用.net实现这一点。只是在catch语句中 代码如下
enter code here
Imports System.Data.SqlClient
Imports System.Data
Public Class dbConn
Private primaryServerLocation As String = "SERVER=primaryAddress;DATABASE=yourDB;User id=youruserID;Password=yourPassword;"
Private secondaryServerLocation As String = "SERVER=secondaryAddress;DATABASE=yourDB;User id=youruserID;Password=yourPassword;"
Public sqlConnection As SqlConnection
Public cmd As SqlCommand
Public Sub primaryConnection()
Try
sqlConnection = New System.Data.SqlClient.SqlConnection(primaryServerLocation)
cmd = New System.Data.SqlClient.SqlCommand()
'test connection
sqlConnection.Open()
sqlConnection.Close()
Catch ex As Exception
secondaryConnection()
End Try
End Sub
Public Sub secondaryConnection()
'Used as the failover secondary server if primary is down.
Try
sqlConnection = New System.Data.SqlClient.SqlConnection(secondaryServerLocation)
cmd = New System.Data.SqlClient.SqlCommand()
'test connection
sqlConnection.Open()
sqlConnection.Close()
Catch ex As Exception
End Try
End Sub
End Class
您好,Joel,谢谢您的回复,即使我没有镜像设置,这也可以吗?我可以使用相同的AD连接字符串来指定故障转移LDAP连接吗?
";Failover Partner=PartnerServerName"
enter code here
Imports System.Data.SqlClient
Imports System.Data
Public Class dbConn
Private primaryServerLocation As String = "SERVER=primaryAddress;DATABASE=yourDB;User id=youruserID;Password=yourPassword;"
Private secondaryServerLocation As String = "SERVER=secondaryAddress;DATABASE=yourDB;User id=youruserID;Password=yourPassword;"
Public sqlConnection As SqlConnection
Public cmd As SqlCommand
Public Sub primaryConnection()
Try
sqlConnection = New System.Data.SqlClient.SqlConnection(primaryServerLocation)
cmd = New System.Data.SqlClient.SqlCommand()
'test connection
sqlConnection.Open()
sqlConnection.Close()
Catch ex As Exception
secondaryConnection()
End Try
End Sub
Public Sub secondaryConnection()
'Used as the failover secondary server if primary is down.
Try
sqlConnection = New System.Data.SqlClient.SqlConnection(secondaryServerLocation)
cmd = New System.Data.SqlClient.SqlCommand()
'test connection
sqlConnection.Open()
sqlConnection.Close()
Catch ex As Exception
End Try
End Sub
End Class