2010-02-03 66 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命名空間/類/屬性非常有用的工具。

哈羅德