返回 sql 服务器实例 returns 空字符串的 WMI 方法
WMI method for returning sql server instances returns empty string
我正在尝试为 return 所有服务器实例创建一个 WMI 方法,但 GetCorrectWmiNameSpace();
return 是一个空字符串。我使用 sql 服务器 2012,知道为什么它 return 是一个空字符串吗?
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()
以支持更高版本的 SQL 服务器,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 服务器的 13、14 等版本做准备。
问题是您使用的是 code that is meant for SQL Server 2008 和旧版本。此方法 GetCorrectWmiNameSpace()
然后 returns string.Empty
即使安装了 SQL Server 2012 或 2014。
我正在尝试为 return 所有服务器实例创建一个 WMI 方法,但 GetCorrectWmiNameSpace();
return 是一个空字符串。我使用 sql 服务器 2012,知道为什么它 return 是一个空字符串吗?
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()
以支持更高版本的 SQL 服务器,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 服务器的 13、14 等版本做准备。
问题是您使用的是 code that is meant for SQL Server 2008 和旧版本。此方法 GetCorrectWmiNameSpace()
然后 returns string.Empty
即使安装了 SQL Server 2012 或 2014。