以编程方式获取 SQL 集群虚拟名称

发布于 2024-08-20 03:00:35 字数 1584 浏览 3 评论 0原文

我编写了一个 Windows 服务来从我们所有的 SQL 服务器收集信息。该服务安装在每台服务器上,并利用 WMI 和 SMO,将相关系统信息插入回中央数据库。为了获取 SQL 信息,我使用以下 C# 代码:

        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 群集虚拟名称。

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);
                    }
                }
            }
        }

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-08-27 03:00:35

使用此代码:

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 Code Creator (http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en),一个非常有用的工具,用于浏览不同的 WMI 命名空间/类/属性。

With this code:

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"]);
}

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

Name: SQL IP Address i (2008CLUSTERSQL)

Name: SQL Network Name (2008CLUSTERSQL)

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

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.

绻影浮沉 2024-08-27 03:00:35

您可以将 WMI 查询与 mscluster WMI 类一起使用吗?

http://technet.microsoft.com/en-us /library/cc780572(WS.10).aspx
http://blogs.msdn.com/clustering/archive/ 2008/01/08/7024031.aspx

通过查询,我的意思是询问所有集群的资源/组以找到 SQL Server 网络名称。

Could you use a WMI query with the mscluster WMI classes?

http://technet.microsoft.com/en-us/library/cc780572(WS.10).aspx
http://blogs.msdn.com/clustering/archive/2008/01/08/7024031.aspx

By query I mean interrogate all the cluster's resources/groups to locate the SQL Server network name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文