0

使用 SQL 查询或 WMI,我想检索 SQL 协议(如 Named Pipes、TCP/IP)是否启用。

我需要为 SQL Server 2000、2005、2008 执行此操作。

谢谢你。

4

1 回答 1

1

WMI Provider for Configuration Management Classes包括WMI 类,它列出了安装在 Microsoft SQL Server 实例上的ServerNetworkProtocol协议,该类位于root\Microsoft\SqlServer\ComputerManagement10命名空间中(对于 sql server 2008,在其他版本中您必须修改命名空间)。

检查这个 C# 示例

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\Microsoft\\SqlServer\\ComputerManagement10", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\Microsoft\\SqlServer\\ComputerManagement10", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM ServerNetworkProtocol");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","ProtocolDisplayName",WmiObject["ProtocolDisplayName"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Enabled",WmiObject["Enabled"]);// Boolean
                    Console.WriteLine("{0,-35} {1,-40}","InstanceName",WmiObject["InstanceName"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","MultiIpConfigurationSupport",WmiObject["MultiIpConfigurationSupport"]);// Boolean
                    Console.WriteLine("{0,-35} {1,-40}","ProtocolName",WmiObject["ProtocolName"]);// String
                    Console.WriteLine();


                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

这将返回类似这样的内容

ProtocolDisplayName                 Shared Memory
Enabled                             True
InstanceName                        MSSQLSERVER
MultiIpConfigurationSupport         False
ProtocolName                        Sm

ProtocolDisplayName                 Named Pipes
Enabled                             False
InstanceName                        MSSQLSERVER
MultiIpConfigurationSupport         False
ProtocolName                        Np

ProtocolDisplayName                 TCP/IP
Enabled                             True
InstanceName                        MSSQLSERVER
MultiIpConfigurationSupport         True
ProtocolName                        Tcp

ProtocolDisplayName                 VIA
Enabled                             False
InstanceName                        MSSQLSERVER
MultiIpConfigurationSupport         False
ProtocolName                        Via
于 2012-04-24T06:49:56.317 回答