Home Forums SQL Server 2012 SQL 2012 - General Get Primary Instance in Availability Group & Create Dynamic Connection String RE: Get Primary Instance in Availability Group & Create Dynamic Connection String

  • Just in case anyone was curious, here's how it turned out:

    I have to provide two connection strings, one for ADO.Net (with MultiSubnetFailover) and one for OleDb.

    I use the ADO.Net connection to connect to an Execute SQL task to get the name of the primary using the dmv Jacob provided with intent on replacing the Data Source in the provided OleDb connection string.

    However I ran into a small hurdle here - 1) we use multiple domains, and 2) we use multiple ports. The DMV provide only supplies HOSTNAME\INSTANCE format so I had to do add code which added any additional information (e.g. domain suffix, portnumber) to the information provided by the DMV.

    Example: If the original OleDb connection string has "Data Source=DbListener.il.global.company.com,1912"

    and the ADO.Net Execute SQL task connects to the listener and using the dmv, it returns "DbNode1\Instance1" for the primary instance...then....

    I need my new OleDb connection string to have "Data Source=DbNode1.il.global.company\Instance1,1912"

    So it took a bit of parsing logic to break off each part, but it's pretty simple if you just split by the delimiters.