在 SQL Server 2005 和 2008 上启用 TCP 的注册表设置是什么? [英] what are registry settings to enable TCP on SQL Server 2005 and 2008?
问题描述
我想以编程方式在 SQL Server 上启用 TCP 连接.我相信我们可以通过修改注册表项并重新启动 SQL Server 服务来实现这一点.我应该编辑哪个注册表?
I want to programatically enable TCP connections on SQL Server. I believe we can achieve this by modifying registry entries and restarting SQL Server service. What registry should I edit?
推荐答案
除非你有充分的理由直接修改注册表,否则我建议你考虑使用 WMI
.WMI 将为您提供更多版本不可知的实现.可以通过 System.Management 命名空间访问 WMI.你可以有类似这样的代码.
Unless you have a good reason for modifying the registry directly, I suggest you consider using WMI
. WMI will provide you with a more version agnostic implementation. WMI can be accessed through the System.Management namespace. You could have code that looks something like this.
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
和以下 using 语句:
This code assumes a project reference to System.Management.dll
and the following using statement:
using System.Management;
Sql 协议 博客有一个 文章,其中详细介绍了上述代码的作用.
The Sql Protocols blog has an article that goes into some detail as to what the above code is doing.
注意:如果防火墙阻止了端口,您仍然无法通过 TCP 访问服务器.
Note: If a firewall is blocking the port(s) you will still be unable to access the server via TCP.
这篇关于在 SQL Server 2005 和 2008 上启用 TCP 的注册表设置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!