Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何以编程方式枚举SQL Server 2005命名实例?_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server 如何以编程方式枚举SQL Server 2005命名实例?

Sql server 如何以编程方式枚举SQL Server 2005命名实例?,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我想以编程方式枚举服务器上的SQL Server 2005命名实例。我知道我应该可以很容易地做到这一点,但我一辈子都不知道该怎么做。提前感谢您的帮助 这是我不久前写的东西……这不完全相同,但它可能会给你一个如何做的想法。这里我列举并检查sql server的express edition,但您应该能够更改它…您需要转到注册表以查找sql server 2005的SKU名称和SKU级别 public static bool IsSqlExpressPresent(string strInstanc

我想以编程方式枚举服务器上的SQL Server 2005命名实例。我知道我应该可以很容易地做到这一点,但我一辈子都不知道该怎么做。提前感谢您的帮助

这是我不久前写的东西……这不完全相同,但它可能会给你一个如何做的想法。这里我列举并检查sql server的express edition,但您应该能够更改它…您需要转到注册表以查找sql server 2005的SKU名称和SKU级别

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;
    }
}