Для этого необходимо использовать поставщика WMI, поставляемого с SMO. Добавить ссылки на
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 [].