在C#中更改Sql Server 2005 TCP / IP端口

时间:2017-02-03 16:24:31

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

我试图从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();
        }

1 个答案:

答案 0 :(得分:0)

Google是你的朋友......

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

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

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#中调用它?