Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Coders,

I want to start SQL server service from my application when the application starts & stop the service when i close the application.

How to do this?

Thanks & Regards.
Posted
Comments
[no name] 15-Oct-12 11:15am    
Use the ServiceController class to do whatever it is that you want to do.

 
Share this answer
 
Comments
vinay.sarmalkar 15-Oct-12 14:33pm    
Dear Mehdi,

The namespace is not showing in vs2010.
Aijaz Ansari 1-Aug-13 7:39am    
thanks...its working
Mehdi Gholam 15-Oct-12 14:42pm    
What namespace?
Shahan Ayyub 19-Oct-12 13:45pm    
you have to add this line at the top of your .vb file:
Imports System.ServiceProcess
System.Diagnostic.Process.Start("sqlservr.exe"); insert it into an event and yr sql server will start whenever you want.
 
Share this answer
 
Comments
Aijaz Ansari 1-Aug-13 7:16am    
not working dude..
try
{
ServiceController controller = new ServiceController();
controller.MachineName = "Machine-Name";
controller.ServiceName = "MSSQL$SQLEXPRESS";

if (controller.Status == ServiceControllerStatus.Running)
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
controller.Start();

}
catch (Exception)
{

}

the above code will stop the Sqlserver.exe if it running other wise Start the same..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900