2017-02-03 101 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对象工作。 –