【问题标题】:Change SQL SERVER EXPRESS 2008 TCP port with Microsoft.SqlServer.Management.Smo使用 Microsoft.SqlServer.Management.Smo 更改 SQL SERVER EXPRESS 2008 TCP 端口
【发布时间】:2010-04-20 19:36:32
【问题描述】:

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

【问题讨论】:

    标签: c# sql smo


    【解决方案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[]。

    【讨论】:

    • 我改了一行:ServerInstance s = c.ServerInstances["SQLEXPRESS"];
    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2011-04-16
      • 1970-01-01
      相关资源
      最近更新 更多