2010-02-03 63 views
2

我已经写了一个Windows服务,从我们所有的SQL服务器收集信息。该服务被安装到每个服务器上,并且利用WMI和SMO将相关的系统信息返回到中央数据库。为了获得SQL信息,我使用以下c#代码:编程越来越SQL群集虚拟名称

 List<Server> sqlServers = new List<Server>(); //List of Smo.Server objects 
     string registrySubKey = @"SOFTWARE\Microsoft\Microsoft SQL Server"; 
     string registryValue = "InstalledInstances"; 

     try 
     { 
      RegistryKey rk = Registry.LocalMachine.OpenSubKey(registrySubKey); 
      string[] instances = (string[])rk.GetValue(registryValue); 
      if (instances.Length > 0) 
      { 
       foreach (string element in instances) 
       { 
        Server s; 
        string serverInstance; 

        if (element == "MSSQLSERVER") //If default instance 
        { 
         serverInstance = System.Environment.MachineName; 
        } 
        else 
        { 
         serverInstance = System.Environment.MachineName + @"\" + element; 
        } 

        s = new Server(serverInstance); 

        if (s != null) 
        { 
         sqlServers.Add(s); 
        } 
       } 
      } 
     } 

我遇到的唯一问题是在我们的SQL群集上。对于那些不熟悉主动/被动sql群集的人,您不能直接连接到其中一个节点。相反,sql群集会获取客户端将连接到的虚拟名称和另一个IP地址。当前的代码将尝试连接到NodeName \ instanceName,这显然不适用于群集。相反,我需要以编程方式查找此节点所属的SQL群集虚拟名称并连接到该虚拟名称。我想我可能能够得到来自MSCluster_Cluster WMI类信息,但只会让我的群集虚拟名称,而不是SQL群集虚拟名称。提前致谢。

回答

1

有了这个代码:

using System.Management; 

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\MSCluster", "SELECT * FROM MSCluster_Resource"); 

foreach (ManagementObject queryObj in searcher.Get()) 
{ 
    Console.WriteLine("Name: {0}", queryObj["Name"]); 
} 

我可以看到我的SQL群集名称(2008CLUSTERSQL)为两个输出:

名称:SQL IP地址I(2008CLUSTERSQL)

名称: SQL网络名称(2008CLUSTERSQL)

这能否帮助?我猜你可以提取出它的名字吗?

我从WMI代码造物主代码(http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en),浏览不同的WMI命名空间/类/属性非常有用的工具。

哈罗德