0

I'm writing an app to automatically restart an SQL service. Basically, these are the steps I am doing:

  1. Close application
  2. Stop SQL service
  3. Start SQL service
  4. Start application

The problem I have is with starting the application. It seems that the application is started before the SQL service thus causing a database connection error. I'm doing checks for the service's status on the following lines but it's still not working:

service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
if (service.Status == ServiceControllerStatus.Stopped)

Code:

static void Main(string[] args)
    {
        PrintMessage("arg 0: " + args[0]);
        PrintMessage("arg 1: " + args[1]);
        PrintMessage("arg 2: " + args[2]);

        Console.Title = "SQL Server Restart";
        PrintMessage("Commencing SQL Server restart. Please wait. . .");

        string serverInstance = args[0];

        ServiceController service = new ServiceController(serverInstance);

        try
        {
            if (service.Status == ServiceControllerStatus.Running)
            {
                PrintMessage("Checking server status");
                TimeSpan timeout = TimeSpan.FromMinutes(5);
                PrintMessage("Stoppping server");
                service.Stop();
                service.Refresh();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                if (service.Status == ServiceControllerStatus.Stopped)
                {
                    PrintMessage("Starting server");
                    service.Start();
                    service.Refresh();
                    service.WaitForStatus(ServiceControllerStatus.Running);

                    if (service.Status == ServiceControllerStatus.Running)
                    {
                        PrintMessage("Server started");

                        //Restart application
                        if (args[2] == "False")
                        {
                            PrintMessage("Press any key to continue. . .");

                            Console.ReadKey();
                            Environment.Exit(0);
                        }
                        else
                        {
                            PrintMessage("Starting Application");
                            ProcessStartInfo info = new ProcessStartInfo();
                            info.FileName = args[1];
                            info.WorkingDirectory = Path.GetDirectoryName(info.FileName);
                            Process process = new Process();
                            process.StartInfo.UseShellExecute = false;
                            Process.Start(info);
                            PrintMessage("Application started");
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine("Base Exception: " + ex.GetBaseException());
            Console.WriteLine("Inner Exception: " + ex.InnerException);

            Console.WriteLine(ex.Message);
        }

        PrintMessage("Press any key to continue. . .");

        Console.ReadKey();


    }
jmc
  • 1,649
  • 6
  • 26
  • 47

1 Answers1

0

Since you say the application runs into a "database connection error", SQL server may simply not be ready when the application tries to connect. Does your code work if you add a 30 second pause just before launching the application?

I have seen this problem with services that signal the Running state too early (bypassing the Starting state). Perhaps SQL server makes the same error.

CoreTech
  • 2,345
  • 2
  • 17
  • 24