Stopping a SQL Server service in Visual Studio using C# code, having problems installing System.ServiceProcess.ServiceController

I want to stop an SQL server service after making a backup of the database, I am using C# code in Visual Studio

Currently the guides I have been trying to use suggest that I use System.ServiceProcesses. So I try to install System.ServiceProcess.ServiceControlle, I then get the following error:

Severity      Code      Description      Project      File      Line      Suppression State Error Could not install package 'System.ServiceProcess.ServiceController 4.4.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Then try to install the Microsoft.NETCore.Targets.NETFramework extension but get the following error:

Severity      Code      Description      Project      File      Line      Suppression State Error Could not install package 'Microsoft.NETCore.Targets.NETFramework 4.6.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

I don't know what is wrong, I even tried installing .NETFramework version 4.5 but was told the installation cannot be done because  ".NET Framework 4.5.2 or a later update is already installed on this computer."

I would really appreciate some help with this, thank you.
LIBRALEXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
You have several options. You can use a SqlConnection and SqlCommand objects that are in the .NET framework and issue a command of "SHUTDOWN WITH NOWAIT" to the SQL Server and it will shutdown if you have sufficient privileges.

You can also use the .NET Framework fully.

there is an assembly that is available since 1.1 called System.ServiceProcess.  Reference that assembly in your project.
then you will or can use a using System.ServiceProcess; in your .cs file.
then you can create your service process with ServiceController controller = new ServiceController("MSSQLSERVER")
You will then want to ensure that you have your dependent services and stop them first.  Below is a sample of type of code that you can test on your dev system to see if it helps, or you can tailor it to your processes. No need for a different version of .NET Framework. 4.5 covers you there.

using System.ServiceProcess;

void StopServices()
{
        ServiceController controller = new ServiceController("MSSQLSERVER");
        foreach(ServiceController c in controller.DependentServices) {
               c.Stop();
        }
        controller.Stop();
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Any special reason to stop SQL Server service?
0
LIBRALEXAuthor Commented:
@DBAduck - Ben Miller
Thank you, I will try using the SQL command and see if I can get that to work. As for the System.ServiceProcess as explained above, I cannot manage to get the extension for that assembly to install, so I cannot make use of it.

@Vitor Montalvão
I am creating a small program that makes a backup of a database and then restore it to a new server. I need to stop SQL server 2005 before I can use the SQL server 2017 database, since there is User issues if I do not stop the SQL2005 server.

We have software that is currently using an sql2005 database and we wish to convert our database to a sql2017 database, the utility I am writing is to be used at our clients to make the transition faster.
0
Shaun VermaakTechnical Specialist/DeveloperCommented:
Did you add a reference to System.ServiceProcess.dll?
0
LIBRALEXAuthor Commented:
Thanks for the help, I managed to stop the SQL service with the "SHUTDOWN WITH NOWAIT" command.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.