How can I change the SQL Server Configurations(TCP/IP, port number,IP Address) using SMO through C#

How can I change the SQL Server Configurations(TCP/IP, port number,IP Address) using SMO through C#

You can configure your sql server and these settings through c#.

first of all you have to add 2 references in your program, that will be available at nuGet package manager.

install them and add in program first.

Go to visual studio->tools->nuget package manager->package manager console

and than install below two references.

Install-Package Microsoft.SQLServer.SMO -Version 150.18068.0-xplat
Install-Package MicrosoftSQLServerSMO2014Wmi -Version 1.0.1

after that include namespaces.


using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Wmi;
namespace ConsoleAppTestDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            //Declare and create an instance of the ManagedComputer    
            ManagedComputer mc = new ManagedComputer("MAHAVEER");

            mc.ConnectionSettings.ProviderArchitecture = ProviderArchitecture.Use64bit; //if instance not found

            Service sqlservice = mc.Services["MSSQL$SQL"];

            if (sqlservice.ServiceState == ServiceState.Running)
            {
                sqlservice.Stop();
            }
            ServerProtocol tcpProtocols = mc.ServerInstances["SQLEXPRESS"].ServerProtocols["tcp"];
            ServerProtocol sharedmemoryProtocols = mc.ServerInstances["SQLEXPRESS"].ServerProtocols["sm"];

            //Enable TCP/IP
            tcpProtocols.IsEnabled = true;

            //Enable Shared Memory Protocol
            sharedmemoryProtocols.IsEnabled = true;

            //Set IP4 port
            tcpProtocols.IPAddresses["IP4"].IPAddressProperties["TcpPort"].Value = "1433";

            //Set IP4 Address
            tcpProtocols.IPAddresses["IP4"].IPAddressProperties["IpAddress"].Value = "127.0.0.1";


            //Set IPAll port
            tcpProtocols.IPAddresses["IPAll"].IPAddressProperties["TcpPort"].Value = "1433";

            //Done Changes
            tcpProtocols.Alter();
            sharedmemoryProtocols.Alter();

            sqlservice.Start();
}
}
}