编程获取SQL群集虚拟名称 [英] Programmatically getting SQL Cluster Virtual Name

查看:153
本文介绍了编程获取SQL群集虚拟名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个Windows服务,从我们所有的SQL服务器收集信息。该服务被安装在每台服务器上,并利用WMI和SMO,插入相关的系统信息反馈给中央数据库。为了获得SQL的信息,我用下面的C#代码:

I have written a Windows service to collect information from all of our SQL servers. The service gets installed onto each server, and utilizing WMI and SMO, inserts relevant system information back to a central database. In order to get the SQL information, I use the following c# code:

        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群集虚拟名称。 。在此先感谢

The only problem I'm having is on our SQL clusters. For those of you unfamiliar with active/passive sql clustering, you cannot connect directly to one of the nodes. Instead the sql cluster gets a virtual name and another ip address that clients would then connect to. The current code I have will try to connect to NodeName\instanceName which obviously will not work on the cluster. Instead I need to programmatically find the SQL cluster virtual name that this node belongs to and connect to that instead. I thought I might be able to get this information from the MSCluster_Cluster WMI class, but that will only get me the cluster virtual name, not the SQL cluster virtual name. Thanks in advance.

推荐答案

有了这个代码:

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)作为两个输出:

I can see my SQL Cluster Name (2008CLUSTERSQL) as two of the outputs:

名称:SQL IP地址时,我(2008CLUSTERSQL)

Name: SQL IP Address i (2008CLUSTERSQL)

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

Name: SQL Network Name (2008CLUSTERSQL)

这能否帮助?我想你可以解压出来它的名字吗?

Will this help? I guess you can extract the name of out it?

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

I got this code from WMI Code Creator (http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en), a very useful tool to browse different WMI namespaces/classes/properties.

哈罗德

这篇关于编程获取SQL群集虚拟名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆