How to add Failover Partner to a connection string in VB.NET

主宰稳场 提交于 2019-12-17 19:31:11

问题


I have a windows application connecting to Database to read some data. Since the database is setup for resilience, my application needs to connect to one of the two databases. Can someone specify the what the syntax would be to specify the failover partner in a connection string using sql server authentication.

Any help is greatly appreciated.


回答1:


Check connectionstrings.com:

Database mirroring
If you connect with ADO.NET or the SQL Native Client to a database that is being mirrored, your application can take advantage of the drivers ability to automatically redirect connections when a database mirroring failover occurs. You must specify the initial principal server and database in the connection string and the failover partner server.

Data Source=myServerAddress;Failover Partner=myMirrorServerAddress;Initial
Catalog=myDataBase;Integrated Security=True;

There is ofcourse many other ways to write the connection string using database mirroring, this is just one example pointing out the failover functionality. You can combine this with the other connection strings options available.




回答2:


If you supply the name of a failover partner server in the connection string, the client will transparently attempt a connection with the failover partner if the principal database is unavailable when the client application first connects.

";Failover Partner=PartnerServerName"

If you omit the name of the failover partner server and the principal database is unavailable when the client application first connects then a SqlException is raised.

Source




回答3:


If you do not have mirroring set up between SQL servers, you can achieve this by using .net. simply in a catch statement.

Code below..

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


来源:https://stackoverflow.com/questions/677678/how-to-add-failover-partner-to-a-connection-string-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!