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



我正在尝试从 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();
        }

谷歌是你的朋友...

如果启用,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#调用它?

最新更新