2010-03-23 3 views

답변

7

레지스트리를 직접 수정할만한 좋은 이유가 없으면 WMI을 사용하는 것이 좋습니다. WMI는 더 많은 버전 독립적 인 구현을 제공합니다. WMI는 System.Management 네임 스페이스를 통해 액세스 할 수 있습니다. 이런 식으로 보이는 코드를 가질 수 있습니다. Sql Protocols 블로그 위의 코드가 무엇을하고 있는지에 관한 몇 가지 세부 사항으로 어울릴 article있다

using System.Management; 

:

public void EnableSqlServerTcp(string serverName, string instanceName) 
{ 
    ManagementScope scope = 
      new ManagementScope(@"\\" + serverName + 
           @"\root\Microsoft\SqlServer\ComputerManagement"); 
    ManagementClass sqlService = 
      new ManagementClass(scope, 
           new ManagementPath("SqlService"), null); 
    ManagementClass serverProtocol = 
      new ManagementClass(scope, 
           new ManagementPath("ServerNetworkProtocol"), null); 

    sqlService.Get(); 
    serverProtocol.Get(); 

    foreach (ManagementObject prot in serverProtocol.GetInstances()) 
    { 
     prot.Get(); 
     if ((string)prot.GetPropertyValue("ProtocolName") == "Tcp" && 
      (string)prot.GetPropertyValue("InstanceName") == instanceName) 
     { 
      prot.InvokeMethod("SetEnable", null); 
     } 
    } 

    uint sqlServerService = 1; 
    uint sqlServiceStopped = 1; 
    foreach (ManagementObject instance in sqlService.GetInstances()) 
    { 
     if ((uint)instance.GetPropertyValue("SqlServiceType") == sqlServerService && 
      (string)instance.GetPropertyValue("ServiceName") == instanceName) 
     { 
      instance.Get(); 
      if ((uint)instance.GetPropertyValue("State") != sqlServiceStopped) 
      { 
       instance.InvokeMethod("StopService", null); 
      } 
      instance.InvokeMethod("StartService", null); 
     } 
    } 
} 

이 코드는 System.Management.dll에 대한 프로젝트 참조 다음과 같은 사용하여 문을 가정합니다.

참고 : 방화벽이 포트를 차단하는 경우에도 TCP를 통해 서버에 액세스 할 수 없습니다.

+0

WMI – Rohit

+0

@Rohit을 사용했지만 코드를 사용하지 않았습니다. 도움이 되었다니 기쁩니다. – VoidDweller

3

HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp 하이브를 살펴보십시오. Enabled, ListenOnAllIPs과 같은 키와 수신 대기 할 IP 주소 목록이 있습니다.