【问题标题】:Change Sql Server 2005 TCP/IP Ports in C#在 C# 中更改 Sql Server 2005 TCP/IP 端口
【发布时间】:2017-02-03 16:24:30
【问题描述】:

我正在尝试从 c# 更改我在 SQL Server 2005 中实例的 TCP/IP 端口和动态端口。 我已经尝试了一个解决方案,如上面的代码所示,但它只有在安装了某些 Server 2008 功能时才有效(例如:SharedManagementObject.msi)。

我需要一个适用于 Sql Server 2005 的解决方案,无需额外安装其他 Sql Server 版本。

这是我已经尝试过的代码(

        try
        {
            Console.WriteLine(" Started...\n");

            const string instanceName = "INSTANCENAME";

            var managedComputer = new ManagedComputer();

            var serviceController = new ServiceController(string.Concat("MSSQL$", instanceName));

            Console.WriteLine("     - Istance: " + instanceName + "\n     - DisplayName: " + serviceController.DisplayName + "\n");

            var serverInstance = managedComputer.ServerInstances[instanceName];

            var serverProtocol = serverInstance.ServerProtocols["Tcp"];

            var ipAddresses = serverProtocol.IPAddresses;

            if (ipAddresses != null)
            {
                for (var i = 0; i < ipAddresses.Count; i++)
                {
                    var ipAddress = ipAddresses[i];

                    if (serviceController.Status == ServiceControllerStatus.Running)
                    {
                        serviceController.Stop();

                        serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
                    }

                    if (!string.Equals(ipAddress.Name, "IPAll"))
                    {
                        ipAddress.IPAddressProperties["Enabled"].Value = true;
                    }
                    ipAddress.IPAddressProperties["TcpDynamicPorts"].Value = "";
                    ipAddress.IPAddressProperties["TcpPort"].Value = "1433";

                    serverProtocol.Alter();
                }
            }

            if (serviceController.Status == ServiceControllerStatus.Running)
            {
                return;
            }

            serviceController.Start();

            serviceController.WaitForStatus(ServiceControllerStatus.Running);

            Console.WriteLine(" Finished...\n");
        }
        catch (Exception exception)
        {
            Console.WriteLine("\n" + exception + "\n");
        }
        finally
        {
            Console.Write(" Press any key to continue... ");
            Console.ReadKey();
        }

【问题讨论】:

    标签: c# sql-server sql-server-2005 tcp


    【解决方案1】:

    Google 是你的朋友...

    如果启用,Microsoft SQL Server 数据库引擎的默认实例将侦听 TCP 端口 1433。SQL Server 数据库引擎和 SQL Server Mobile 的命名实例配置为动态端口,这意味着它们选择可用端口当 SQL Server 服务启动时。通过防火墙连接到命名实例时,将数据库引擎配置为侦听特定端口,以便可以在防火墙中打开相应的端口。

    将 TCP/IP 端口号分配给 SQL Server 数据库引擎

    In SQL Server Configuration Manager, in the console pane, expand SQL Server 2005 Network Configuration, expand Protocols for <instance name>, and then double-click TCP/IP.
    
    In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear, in the format IP1, IP2, up to IPAll. One of these are for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each address, and then click Properties to identify the IP address that you wish to configure.
    
        If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0.
    
        In the IPn Properties area box, in the TCP Port box, type the port number you wish this IP address to listen on, and then click OK.
    
        In the console pane, click SQL Server 2005 Services.
    
        In the details pane, right-click SQL Server (<instance name>) and then click restart, to stop and restart SQL Server.
    After you have configured SQL Server to listen on a specific port there are three ways to connect to a specific port with a client application:
    
        Run the SQL Server Browser service on the server to connect to the Database Engine instance by name.
    
        Create an alias on the client, specifying the port number.
    
        Program the client to connect using a custom connection string.
    

    现在用 C# 来做,我不知道。也许尝试一个 BAT 文件并从 C# 调用它?

    【讨论】:

    • 是的,我知道我可以从配置管理器手动完成,我需要 C# 中的解决方案来自动完成!我已经找到了一个解决方案,但它只能通过使用 SqlServer 2008 对象来工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2010-09-25
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    相关资源
    最近更新 更多