blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Merrick Chaffer's Blog

How to retrieve the database connection string from a Team Foundation Server Client

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;

        }

Published 06 March 2007 15:14 by merrick.chaffer

Comments

 

dengia said:

This example works fine on TFS 2005, but on TFS 2008 we found it works fine only for domain admin (???), for other domain users it returns empty string.

Even if I test Registration web service http://OurServer:8080/services/v1.0/Registration.asmx?op=GetRegistrationEntries I get same result: for domain admin Databases xml tag has value, for other domain users this xml tag is empty.

Any ideas?

May 22, 2008 10:36
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems