C # start stop SQL database service method one:
To fill in the command line: NET Start/stop MSSQLServer
C # start stop SQL database service method two:
Implemented through C # code;
using System;
using System.Windows.Forms;
using System.ServiceProcess;
class Test:form
{
Test ()
{
//Start button;
Button btn1 = New button ();
Btn1. Parent = this;
Btn1. Text = "Start SQL Server";
Btn1. Tag = "START";
Btn1. top=50;
Btn1. left = 20;
Btn1. Width = 120;
Btn1. Click + + new EventHandler (Btnclick);
//caption;
Label La=new label ();
La. Text= "C # start SQL Service";
La. Forecolor=system. Drawing.Color.Green;
La. left=100;
La. top=20;
La. Parent=this;
//Close button;
Button btn2 = New button ();
Btn2. Parent = this;
Btn2. Text = "Stop sql Server";
Btn2. Tag = "STOP";
Btn2. left = btn1. Right + 10;
Btn2. top=50;
Btn2. Width = 120;
Btn2. Click + + new EventHandler (Btnclick);
This. Text= "C # start SQL Service";
}
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 ();
MessageBox.Show (SQL database service started successfully!) "," prompt Information ");
}
if (s = = "STOP" &&!sc. Status.equals (servicecontrollerstatus.stopped))
{
SC. Stop ();
MessageBox.Show (The SQL database service is down!) "," prompt Information ");
}
}
static void Main ()
{
Application.Run (new Test ());
}
}
Effect as shown:
C # start to stop SQL database services related to the introduction here, I hope you like.