2017-02-03 96 views
0

我試圖從c#中更改SQL Server 2005中實例的TCP/IP端口和動態端口。 我已經嘗試瞭解決方案,如上面的代碼,但它只有在安裝了某些Server 2008功能時才起作用(如:SharedManagementObject.msi)。更改C#中的SQL Server 2005 TCP/IP端口#

我需要一個適用於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(); 
     } 

回答

0

谷歌是你的朋友...

如果啓用,微軟的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#調用它?

+0

是的,我知道我可以從配置管理器手動執行它,我需要在C#中的解決方案自動執行它!我已經找到了一個解決方案,但它只能使用SqlServer 2008對象工作。 –