【问题标题】:WMI method for returning sql server instances returns empty string用于返回 sql server 实例的 WMI 方法返回空字符串
【发布时间】:2015-05-20 07:45:16
【问题描述】:

我正在尝试创建一个 WMI 方法来返回所有服务器实例,但 GetCorrectWmiNameSpace(); 返回一个空字符串。我使用 sql server 2012,知道为什么它返回一个空字符串吗?

public bool  EnumerateSQLInstances()
{

    string _instanceName = string.Empty;
    string _serviceName = string.Empty;
    string _version = string.Empty;
    string _edition = string.Empty;
    string _correctNamespace = GetCorrectWmiNameSpace();
    if (string.Equals(_correctNamespace, string.Empty))
    {
        return false;
    }

    string query = string.Format("select * from SqlServiceAdvancedProperty where SQLServiceType = 1 and PropertyName = 'instanceID'");
    ManagementObjectSearcher getSqlEngine = new ManagementObjectSearcher(_correctNamespace, query);
    if (getSqlEngine.Get().Count == 0)
    {
        return false;
    }
    foreach (ManagementObject sqlEngine in getSqlEngine.Get())
    {
        _serviceName = sqlEngine["ServiceName"].ToString();
        _instanceName = GetInstanceNameFromServiceName(_serviceName);
        _version = GetWmiPropertyValueForEngineService(_serviceName, _correctNamespace, "Version");
        _edition = GetWmiPropertyValueForEngineService(_serviceName, _correctNamespace, "SKUNAME");
    }

    txtResponse.Text += _serviceName.ToString() + ", " + _instanceName.ToString() + ", " + _version.ToString() + ", " + _edition.ToString();

    return true;
}

public static string GetCorrectWmiNameSpace()
{
    String wmiNamespaceToUse = "root\\Microsoft\\sqlserver";
    List<string> namespaces = new List<string>();
    try
    {
        // Enumerate all WMI instances of
        // __namespace WMI class.
        ManagementClass nsClass =
            new ManagementClass(
            new ManagementScope(wmiNamespaceToUse),
            new ManagementPath("__namespace"),
            null);
        foreach (ManagementObject ns in
            nsClass.GetInstances())
        {
            namespaces.Add(ns["Name"].ToString());
        }
    }
    catch (ManagementException e)
    {
        Console.WriteLine("Exception = " + e.Message);
    }
    if (namespaces.Count > 0)
    {
        if (namespaces.Contains("ComputerManagement10"))
        {
            //use katmai+ namespace
            wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement10";
        }
        else if (namespaces.Contains("ComputerManagement"))
        {
            //use yukon namespace
            wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement";
        }
        else
        {
            wmiNamespaceToUse = string.Empty;
        }
    }
    else
    {
        wmiNamespaceToUse = string.Empty;
    }
    return wmiNamespaceToUse;
}

【问题讨论】:

  • 也显示GetCorrectWmiNameSpace();的方法代码
  • 您知道wbemtest 实用程序吗?打开Windows Run &gt; Type wbemtest
  • 我真的看不出您的方法如何返回null。你确定没有返回空字符串吗?
  • 这段代码是针对2008版本的,可能和2012不同。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# winforms sql-server-2012 wmi wmi-query


【解决方案1】:

您必须修改方法GetCorrectWmiNameSpace() 以支持更高版本的SQL Server for 2012 and 2014 it will be

       if (namespaces.Count > 0)
        {
            if (namespaces.Contains("ComputerManagement10"))
            {
                //use katmai+ namespace
                wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement10";
            }
            else if (namespaces.Contains("ComputerManagement"))
            {
                //use yukon namespace
                wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement";
            }
            else if (namespaces.Contains("ComputerManagement11"))
            {
                //use 2012 + namespace
                wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement11";
            }
            else if (namespaces.Contains("ComputerManagement12"))
            {
                //use 2014 + namespace
                wmiNamespaceToUse = wmiNamespaceToUse + "\\ComputerManagement12";
            }
            else
            {
                wmiNamespaceToUse = string.Empty;
            }
        }

或者您可以编写更灵活的代码,为 SQL Server 的 13、14 ... 等版本做好准备。

问题是您使用了code that is meant for SQL Server 2008 和旧版本。即使安装了 SQL Server 2012 或 2014,此方法 GetCorrectWmiNameSpace() 也会返回 string.Empty

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多