以编程方式获取 SQL 集群虚拟名称
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此代码:
我可以看到我的 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:
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.
您可以将 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.