Click here to Skip to main content
15,884,745 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I want to restart Sql server from command prompt using these following command:
NET STOP MSSQL$SQLExpress
NET START MSSQL$SQLExpress

I have found these following code which start cmd process. But i have no idea how to run above command on command prompt using c#
C#
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.StartupPath.Trim() + @"\command\cmd.exe";
psi.Arguments = "NET STOP MSSQL$SQLExpress";
p.StartInfo = psi;
p.Start();

please help me to restart SQL server.
Thanks in advance
Posted
Updated 10-Jan-12 23:47pm
v3
Comments
Shahin Khorshidnia 11-Jan-12 5:47am    
Good question.

When there are some classes to do this why you are using command prompt ?

C#
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}



extracted from here :
http://www.csharp-examples.net/restart-windows-service/[^]

To know the name of the SQL Server service go to control panel or see the output of this command :
sc query | more
 
Share this answer
 
Comments
sahabiswarup 13-Jan-12 2:27am    
after trying the above code i have found the following error though SQL server name is MSSQLSERVER.

{System.InvalidOperationException: Cannot open MSSQLSERVER service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Stop()
at RestartSQL.Form1.RestartService(String serviceName, Int32 timeoutMilliseconds) in D:\Biswarup\Testing\CMD\RestartSQL\RestartSQL\Form1.cs:line 69}
C#
Process.Start("NET STOP MSSQL$SQLExpress");
 
Share this answer
 
Comments
sahabiswarup 11-Jan-12 5:34am    
no need to mention other parameter?
Andreas Gieriet 12-Jan-12 15:05pm    
Usually this is sufficient.

The only problem: the net command returns before the stop got fully completed. So, if you immediately try to e.g. delete the DB files, you might fail with an access violation. Adding an arbitrary delay does only reduce the risk that this happens. The more reliable way in connection with services start/stop is to do it over the ServiceController class and wait there until the service reports that it is stopped.

The code must be a bit sophisticated, e.g. pseudo-code for stopping the service:


if (!stopped)
{
if (!stopping)
{
stop()
}
wait-for-stopped
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900