Method 1:
Run the CMD command in C #. Net start/stop MSSQLServer
Method 2:
// Use the Windows service controller to start or stop the service
Using system;
Using system. Windows. forms;
Using system. serviceprocess;
Class test: Form
{
Test ()
{
Button btn1 = new button ();
Btn1.parent = this;
Btn1.text = "start SQL Server ";
Btn1.tag = "start ";
Btn1.left = 10;
Btn1.width = 120;
Btn1.click + = new eventhandler (btnclick );
Button btn2 = new button ();
Btn2.parent = this;
Btn2.text = "Stopping SQL Server ";
Btn2.tag = "stop ";
Btn2.left = btn1.right + 10;
B tn2.width = 120;
Btn2.click + = new eventhandler (btnclick );
}
Void btnclick (Object sender, eventargs E)
{
String S = (sender as button). Tag. tostring ();
Servicecontroller SC = new servicecontroller ("MSSQLServer ");
If (S = "start" & SC. Status. Equals (servicecontrollerstatus. Stopped ))
{
SC. Start ();
}
If (S = "stop "&&! SC. Status. Equals (servicecontrollerstatus. Stopped ))
{
SC. Stop ();
}
}
Static void main ()
{
Application. Run (new test ());
}
}