Determining the Configuration Database in a SharePoint 2007 Farm

For whatever reason you may have, you may find the need to identify the Configuration Database in your server farm programmatically.  The problem is that it is not directly exposed for consumption via the object model.  What I mean by this, is that there is no single identifiable property that tells you that the database your enumerating is in fact the Configuration Database.

In V2, it was a simple matter of using the SPConfigDatabase object within the Microsoft.SharePoint.Administration namespace, but with V3, this was marked obsolete, with no reference to  what you can use in V3.  Another issue is that the new object type for the configuration database (SPConfigurationDatabase) is not exposed publicly in the object model.

I’ve seen a few posts that drive you to towards deriving it using crazy nested reflection by first starting with the ProcessIdentity object of the farms TimerService, but I recommend a clearer and simpler approach as follows

// Code Begins
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPTest
{
    class Program
    {
        static void Main(string[] args)
        {

            // Get the Database Service class within the farm
            SPDatabaseService dbService = SPFarm.Local.Services.GetValue();

            // Enumerate the service instances
            foreach(SPDatabaseServiceInstance instance in dbService.Instances)
            {
                // enumerate the databases within this service instance
                foreach (SPDatabase database in instance.Databases)
                {
                    Console.WriteLine("Database Server: {0} :: Database Name: {1}",database.Server.Name, database.Name);

                    object oDB = database.GetType();

                    Console.WriteLine("Object Type: {0}", oDB.ToString());

                    // If the objects type is "Microsoft.SharePoint.Administration.SPConfigurationDatabase, you've found the Config DB.
                    if (oDB.ToString().CompareTo("Microsoft.SharePoint.Administration.SPConfigurationDatabase") == 0)
                        Console.WriteLine("You found the config database oDB.ToString(): {0} ",oDB.ToString());
                }
            }
        }
    }
}
// Code Ends
 

HTH!

– Keith

5 Replies to “Determining the Configuration Database in a SharePoint 2007 Farm”

  1. Cool, thanks for the code write up Keith.

    I think last time I did this I cheated with REG QUERY and “HKLM\Software\Microsoft\Shared Tools\Web Server Extensions\12.0\Secure\ConfigDb” but that was more Admin and Dev work so it fit.

  2. Thanks this is supportive. Very Helpful Post on Determining the Configuration Database in a SharePoint 2007 Farm.

    Thank you for the info. Keep it up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: