How to enable TCP protocol of SQL Server Express 2019 through C# code?

Anoop Choudhary 20 Reputation points
2024-12-04T07:17:35.38+00:00

I have MS SQLExpress 2019 installed in Windows 11 desktop. I want to enable SQLExpress TCP protocol programmatically from Windows Application using c#.

So, I have written following simple code.

using Microsoft.SqlServer.Management.Smo.Wmi;
ManagedComputer mc = new ManagedComputer();
###1st way###
ServerProtocol srvprcl = mc.ServerInstances[0].ServerProtocols[2];
srvprcl.IsEnabled = true;
srvprcl.Alter();
###2nd way###
Urn uri = new Urn("ManagedComputer[@Name='computer-name']/ServerInstance[@Name='SQLEXPRESS']/ServerProtocol[@Name='Tcp']");
ServerProtocol srvprcl = (ServerProtocol)mc.GetSmoObject(uri);
srvprcl.IsEnabled = true;
srvprcl.Alter();	

This code is returning mc.ServerInstances.count = 0, so eventually I am getting error, though this code is running with administrative permission.

In the alternate way same thing is happening when I am passing urn object.

To get around I have created following PowerShell script.

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name SqlServer -AllowClobber -Force
Import-Module SQLServer -Force
$wmi = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
$Wmi
$uri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='SQLEXPRESS']/ServerProtocol[@Name='Tcp']"
$Tcp = $wmi.GetSmoObject($uri)
$Tcp.IsEnabled = $true
$Tcp.Alter()
$Tcp

With administrative permission, in the manual run, this PowerShell script ran well and it enabled TCP protocol.

Because I need to automate this process using C# code, so I have written C# code and ran above mentioned script from Windows Application with Administrative code.

Here I have received an error. From the investigation, I have found that $Wmi object is returning zero ServerInstances.

When I was running script manually then I was getting {SQLEXPRESS} from output of $Wmi ServerInstances but this not happening when same script is running from C# code.

I think this problem will be related to code access security but not sure how to resolve it. Any help will be appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,915 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,012 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,223 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,157 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,708 questions
{count} votes

Accepted answer
  1. gekka 10,566 Reputation points MVP
    2024-12-05T09:04:28.81+00:00

    As far as I tried, I am able to get ServerInstances only when running on 64bit program.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.