c# - 使用 Microsoft.SqlServer.Management.Smo 更改 SQL SERVER EXPRESS 2008 TCP 端口

标签 c# sql smo

我需要在 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/

相关文章:

c# - Linq 查询 - 列上不同的数据类型

c# - 匿名类型属性 setter

java - 使用实体多对多定义的 Hibernate HQL 投影问题

c# - 服务器还原数据库失败

powershell - 使用PowerShell和SMO库从.BAK文件创建新数据库

c# - 在 C# 中定位安装程序路径

mysql - 在 where 子句中无效使用没有聚合函数的组函数

mysql - 基于多个条件和 JOINS 进行一对多求和

Powershell 函数限制数据类型

c# - 以下方法或属性之间的调用不明确 : 'EntitiesLan.EntitiesLan()' and 'EntitiesLan.EntitiesLan()'