SQL Server configuration database mirroring, there may be friends will be more confused, you do two database out, their IP address is not the same, when the database switch past, my database connection string can be good? Do you have to control which database is connected in the code?
In fact, the problem is that using ADO or SQL Native client to automatically connect to a failover partner, the connection string looks like this:
Connectionstring= "DATASOURCE=A; Failoverpartner=b;initial catalog=adventureworks;integrated security=true; "
Datasource= A; This is the IP address of the primary database that we commonly use, Failover partner=b; This fills in the IP address of the mirror database, and once a connection error occurs, ADO automatically connects to the mirrored database after the timeout.
SqlConnectionStringBuilder sqlsb = new SqlConnectionStringBuilder ();
SQLSB. DataSource = "ServerA";
SQLSB. Failoverpartner = "ServerB";
SQLSB. UserID = "XXX";
SQLSB. Password = "";
SQLSB. InitialCatalog = "DataBase";
SqlConnection sqlconn = new SqlConnection ();
Sqlconn. ConnectionString = sqlsb. ConnectionString;
String activeserver = sqlconn. DataSource;
Failover automatic connection for SQL Server database mirroring