When we copy, rename, move, or delete SQL Server, the system cannot directly perform operations because the SQL service is enabled, and the service must be stopped, after the operation is complete, enable it again. You can use SQL Server Management studio or net start/stop MSSQLServer in cmd to stop and enable SQL Server. In the C # program, you can use either of the following methods to enable or disable the SQL Service:
Method 1: use C # To execute the CMD statement Net start/stop MSSQLServer. This method is not described in detail.
Method 2: process control.
Add reference first
Select the. NET control system. serviceprocess
After adding a reference, you can use it. First, in the program Header
Code:
- Using system. serviceprocess;
Then create Process Control for SQL Server
Code:
- Servicecontroller SC = new servicecontroller ("MSSQLServer ");
You need to stop the service.
Code:
- SC. Stop ();
Use it at the beginning
Code:
- SC. Start ();