The following code will get the correct database and server name for the Team Foundation Server Work Item Tracking database. Note in the call to GetRegistrationEntries you can use any one of the following types…
<Type>vstfs</Type>
<Type>Build</Type>
<Type>Reports</Type>
<Type>Wss</Type>
<Type>WorkItemTracking</Type>
<Type>VersionControl</Type>
<Type>TestTools</Type>
UICredentialsProvider defaultUICredentialsProvider = new UICredentialsProvider();
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("myServerName", defaultUICredentialsProvider);
try
{
tfs.EnsureAuthenticated();
}
catch (TeamFoundationServerUnauthorizedException ex)
{
throw ex;
}
string connectionString;
if (GetWitDatabaseConnectionString(tfs, out connectionString))
{
Debug.WriteLine("Connection string is '" + connectionString + "'");
}
else
{
Debug.WriteLine("Failed to find connection string");
}
/// <summary>
/// Gets the work item tracking (wit) database connection string from a Team Foundation Server.
/// </summary>
/// <param name="tfs">The TFS.</param>
/// <param name="connectionString">The connection string.</param>
/// <returns>True - if found otherwise false</returns>
private bool GetWitDatabaseConnectionString(TeamFoundationServer tfs, out string connectionString)
{
bool blnFoundDB = false;
connectionString = null;
IRegistration registrationService = (IRegistration)tfs.GetService(typeof(IRegistration));
RegistrationEntry[] registrationEntries = registrationService.GetRegistrationEntries("WorkItemTracking");
if (registrationEntries.Length > 0)
{
foreach (RegistrationEntry regEntry in registrationEntries)
{
foreach (Database database in regEntry.Databases)
{
if (database.Name == "WIT DB")
{
connectionString = database.ConnectionString.Replace("@SQLServerName@", database.SQLServerName).Replace("@DatabaseName@", database.DatabaseName);
blnFoundDB = true;
break;
}
}
if (blnFoundDB)
{
break;
}
}
}
return blnFoundDB;
}