C # start stop SQL database service method

Source: Internet
Author: User
Tags mssqlserver

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.