SQLServer Service Manager

Source: Internet
Author: User

Using System;
Using System. Configuration;
Using System. Diagnostics;
Using System. Threading;
Using System. ServiceProcess;
Using System. Collections;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
 
Start or stop the Windows service using namespace
{
Public partial class Form1: Form
{
String tempServer;
String tempProcess;
Public Form1 ()
{
InitializeComponent ();
Try
{
TempServer = System. Configuration. ConfigurationManager. receivettings ["servername"];
TempProcess = System. Configuration. ConfigurationManager. AppSettings ["processname"]; // used to read Configuration data from the Configuration file XML
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
 
Private void btnSartSQLServer_Click (object sender, EventArgs e) // start the service
{
Try
{
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Running) // judge whether the service is Running
{
MessageBox. Show ("the service is running, do not start manually", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
Label1.Text = "starting... please wait ......";
Thread t = new Thread (Start); // open a new Thread to Start the service, so that the window will not be suspended
T. Start ();
}
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Public void Start () // The function used to Start the service.
{
Try
{
ServiceController SC = new ServiceController (tempServer );
SC. Start ();
String name = SC. ServiceName; // get the service runtime name
Thread. Sleep (4000); // Sleep for four seconds, waiting for the service to start
Label1.Text = "service started successfully ";
Process [] process = Process. GetProcessesByName (tempProcess); // run the Process to obtain the PID of the process.
Process p = process [0];
Int num = p. Id;
Label2.Text = "service name" + name + ", PID is" + num;
MessageBox. Show ("started successfully", "succeeded", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Private void btnStopSQLServer_Click (object sender, EventArgs e) // stop the service
{
Try
{
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Stopped) // determines whether the service is Stopped.
{
MessageBox. Show ("the service itself stops, do not stop manually", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
Label1.Text = "Stopping... please wait ......";
Thread t = new Thread (Stop );
T. Start ();
}
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Public void Stop () // Stop a service function
{
Try
{
ServiceController SC = new ServiceController (tempServer );
SC. Stop ();
Thread. Sleep (4000); // wait for the service to stop after four seconds of Sleep
Label1.Text = "Service stopped successfully ";
Label2.Text = "";
MessageBox. Show ("stopped successfully", "succeeded", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
 
Private void Form1_Load (object sender, EventArgs e) // form loading event
{
Try
{
Control. checkforillegalcrossthreadcils = false;
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Running ))
{
Label1.Text = "the service is running ";
Process [] process = Process. GetProcessesByName (tempProcess );
Process p = process [0];
Int num = p. Id;
String name = SC. ServiceName;
Label2.Text = "service name:" + name + ", PID:" + num;
}
Else
Label1.Text = "the Service has stopped. Please start the service ";
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
 
Private void button#click (object sender, EventArgs e)
{
SQLServer service manager. AboutBox1 a = new SQLServer service manager. AboutBox1 ();
A. ShowDialog ();
}
}
}
/*--------------------------------------------------------------------------------*/


 

From kay2010hh

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.