SQL服务未通过ServiceController正常启动

本文关键字:常启动 启动 ServiceController 服务 SQL | 更新日期: 2023-09-27 18:16:26

我正在编写一个应用程序来自动重启SQL服务。基本上,这些是我正在做的步骤:

  1. 关闭应用程序
  2. 停止SQL服务
  3. 启动SQL服务
  4. 启动应用程序

我遇到的问题是启动应用程序。似乎应用程序在SQL服务之前启动,从而导致数据库连接错误。我正在检查以下几行服务的状态,但它仍然不工作:

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

代码:

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();

    }

SQL服务未通过ServiceController正常启动

既然您说应用程序运行时遇到了"数据库连接错误",那么当应用程序尝试连接时,SQL server可能只是没有准备好。如果在启动应用程序之前添加30秒的暂停,代码还能正常工作吗?

我已经看到这个问题的服务信号运行状态太早(绕过启动状态)。