2

我需要在 c# 中更改 SQL EXPRESS 2008 实例的默认端口(1433)。

4

2 回答 2

1

通常你通过他们的 UI 重新分配端口

http://msdn.microsoft.com/en-us/library/ms177440.aspx

但是,我认为它只是将其保留在注册表中,因此最简单的方法可能是在 UI 中将其更改为某个特定数字(例如 12345),然后在 HKLM\SOFTWARE\Microsoft\Microsoft SQL Server 下查找数字。这样做我自己会显示密钥是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\Tcp\IP1,其 REG_SZ(似乎很奇怪)值名为 TcpPort。

如果您的意思是(或还需要知道)如何连接到非默认端口上的默认实例,只需将源从主机更改为主机端口(例如,将 FOO 更改为 FOO,12345)

http://msdn.microsoft.com/en-us/library/ms191260.aspx

于 2010-04-20T19:48:31.313 回答
1

您必须使用 SMO 附带的 WMI 提供程序来执行此操作。添加引用到

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlWmiManagement
Microsoft.SqlServer.WmiEnum

using一个

using Microsoft.SqlServer.Management.Smo.Wmi;

那么代码基本上是这样的:

ManagedComputer c = new ManagedComputer();

//Get the SQL service and stop it if it's running
Service svc = c.Services["MSSQL$SQLEXPRESS"];
if (svc.ServiceState == ServiceState.Running)
{
    svc.Stop();
}

//Connect to the SQLEXPRESS instance and change the port
ServerInstance s = c.ServerInstances["MSSQL$SQLEXPRESS"];
ServerProtocol prot = s.ServerProtocols["Tcp"];
prot.IPAddresses[0].IPAddressProperties["TcpPort"].Value = "1433";

//Commit the changes
prot.Alter();

//Restart the service
svc.Start();

这假设您有一个 IP 地址而不是多个地址。如果您有多个,您可能需要将索引修改为 prot.IPAddresses[]。

于 2010-04-20T22:00:22.817 回答