Hi,
I want to be able to run, pause or stop a SQL Service (as in SQL Server Service Manager) from within a C# application. How can I do this
The server I'm using is called DEANHPLAPTOP. I'm sure this can be done through the command line somehow in DOS but how can I do it in an application
The end result I want is to have it so that the first form in the app (login form) starts the service if it's not already running and then stops it when the application is closed.

SQL Server on C# question
odrejesus
JMCD73
// Determine if SQL Server is running
ServiceController serviceControllerSQL = new ServiceController("MSSQLSERVER");
if (serviceControllerSQL.Status != ServiceControllerStatus.Running |
serviceControllerSQL.Status == ServiceControllerStatus.Stopped |
serviceControllerSQL.Status == ServiceControllerStatus.Paused)
{
// If SQL Server 2000 3a is not running, paused or stopped start the service
serviceControllerSQL.Start();
}
I'd like to now do all of this checking before each query is run on the database. Do I really need to do this I also want to run the SQL Server Service check everytime anything is done on a form (is this done by using the validate method ).
Evan2
Willy.
emirac
Dean,
You can use the Status property on the ServiceController instance to figure out whether the service is started, stopped, etc, etc.
Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
Parikshit A. Bhinde
Thanks for your help.
BenWillett
The type or namespace name 'ServiceController' does not exist in the namespace 'System.ServiceProcess' (are you missing an assembly reference )
TITRA FILM
You can then call .Start() and .Stop() on that object.
Incidentally in cmd, it's net start <shortservicename> and net stop <shortservicename>.
ruiefree
I have now got the reference to System.ServiceProcess.dll thanks that WAS obvious LOL...
I have now got :
"
ServiceController serviceController = new ServiceController("MSSQLSERVER");
serviceController.Start();
serviceController.Pause();
serviceController.Stop();
"
How can I do an if statement e.g.
if (serviceController is stopped)
{ START IT }
if(serviceController is paused)
{ START IT }
if (serviceController is running)
{ STOP IT }
In my application i have a login form I have alredy designed and got working fine. Before I run each SQL query I have to determine if the service is running and my DB exists. How can I do this.
Also how can I get the state of the service E.g. Stopped, Running, Paused.
xizzy44
Are you missing a reference to System.ServiceProcess.dll
Regards,
DeepakKapoor.NET