我需要在 C# 中更改 SQL EXPRESS 2008 实例的默认端口 (1433)。
最佳答案
您必须使用 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[]。
关于c# - 使用 Microsoft.SqlServer.Management.Smo 更改 SQL SERVER EXPRESS 2008 TCP 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2678067/