1

I want to programmatically enumerate the SQL Server 2005 named instances on a server. I know I should be able to do this fairly easily, but I can't for the life of me figure out how. Thanks in advance for any help!

2 Answers 2

1

Here is what I had written a while ago...This is not exactly the same but it may give you an idea on how to do it. Here I am enumerating and checking for the express edition of the sql server, but you should be able to change that...You will need to go to the registry to find the SKUNAME and SKULEVEL of the sql server 2005.

public static bool IsSqlExpressPresent(string strInstance, Utility.Log log)
{
    const string edition = "Express Edition";
    string instance = "MSSQL$" + strInstance.ToUpper();
    const int spLevel = 1;

    bool fCheckEdition = false;
    bool fCheckSpLevel = false;

    try
    {
        // Run a WQL query to return information about SKUNAME and SPLEVEL about installed instances
        // of the SQL Engine.
        ManagementObjectSearcher getSqlExpress =
                new ManagementObjectSearcher("root\\Microsoft\\SqlServer\\ComputerManagement",
                "select * from SqlServiceAdvancedProperty where SQLServiceType = 1 and ServiceName = '"
                + instance + "' and (PropertyName = 'SKUNAME' or PropertyName = 'SPLEVEL')");

        // If nothing is returned, SQL Express isn't installed.
        if (getSqlExpress.Get().Count == 0)
        {
            return false;
        }

        // If something is returned, verify it is the correct edition and SP level.
        foreach (ManagementObject sqlEngine in getSqlExpress.Get())
        {
            if (sqlEngine["ServiceName"].ToString().Equals(instance))
            {
                switch (sqlEngine["PropertyName"].ToString())
                {
                    case "SKUNAME":
                        // Check if this is Express Edition or Express Edition with Advanced Services
                        fCheckEdition = sqlEngine["PropertyStrValue"].ToString().Contains(edition);
                        break;

                    case "SPLEVEL":
                        // Check if the instance matches the specified level
                        fCheckSpLevel = int.Parse(sqlEngine["PropertyNumValue"].ToString()) >= spLevel;
                        //fCheckSpLevel = sqlEngine["PropertyNumValue"].ToString().Contains(spLevel);
                        break;
                }
            }
        }

        if (fCheckEdition & fCheckSpLevel)
        {
            return true;
        }
        return false;
    }
    catch (ManagementException e)
    {
        if (log != null)
        {
            log.Write(
               Utility.LogState.Error,
               "Database",
               "Could not find OfficeClip instance of SqlExpress: " + e.ErrorCode + ", " + e.Message
               );
        }
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

http://betav.com/blog/billva/2006/09/question_of_the_day_enumeratin_1.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.