简体   繁体   中英

Change Sql Server 2005 TCP/IP Ports in C#

I'm trying to change from c# the TCP/IP ports and dynamic ports of my instance in SQL Server 2005. I have already tried a solution, as in the code above, but it works only if some Server 2008 functionality are installed (like: SharedManagementObject.msi).

I need a solution that work for Sql Server 2005 without the additional installation of other Sql Server editions.

Here is the code that i have already tried (

        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();
        }

Google is your friend...

If enabled, the default instance of the Microsoft SQL Server Database Engine listens on TCP port 1433. Named instances of the SQL Server Database Engine and SQL Server Mobile are configured for dynamic ports, which means they select an available port when the SQL Server service is started. When connecting to a named instance through a firewall, configure the Database Engine to listen on a specific port, so that the appropriate port can be opened in the firewall.

To assign a TCP/IP port number to the SQL Server Database Engine

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.

Now to do that in C#, I don't know. Maybe try a BAT file and call it from C#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM