Esempio n. 1
0
        private static bool CheckSqlServer(ServiceController server)
        {
            if (server.Status != ServiceControllerStatus.Running)
            {
                WindowHelper.HandleError("The {0} instance of SQL Server is not running, it is in {1} state".FormatWith(server.ServiceName, server.Status), false);
                return(false);
            }

            return(true);
        }
        public static bool CheckSqlServer()
        {
            if (!CheckSqlServerState.Value)
            {
                return(true);
            }

            try
            {
                using (new ProfileSection("Check SQL Server"))
                {
                    var profile = ProfileManager.Profile;
                    Assert.IsNotNull(profile, "Profile is unavailable");

                    var ds   = new SqlConnectionStringBuilder(profile.ConnectionString).DataSource;
                    var arr  = ds.Split('\\');
                    var name = arr.Length == 2 ? arr[1] : String.Empty;

                    var serviceName = GetSqlServerServiceName(profile.ConnectionString);
                    if (String.IsNullOrEmpty(serviceName))
                    {
                        WindowHelper.HandleError("The {0} instance of SQL Server cannot be reached".FormatWith(ds), false);
                        return(ProfileSection.Result(false));
                    }

                    ServiceController[] serviceControllers = ServiceController.GetServices();
                    ServiceController   server             = (!String.IsNullOrEmpty(name) ? serviceControllers.FirstOrDefault(s => s.ServiceName.EqualsIgnoreCase(name)) : null) ??
                                                             serviceControllers.FirstOrDefault(
                        s => s.ServiceName.EqualsIgnoreCase(serviceName)) ??
                                                             serviceControllers.FirstOrDefault(s => s.ServiceName.EqualsIgnoreCase(serviceName));
                    Assert.IsNotNull(server, "Cannot find the " + (name.EmptyToNull() ?? "default") + " sql server instance");
                    var result = CheckSqlServer(server);

                    return(ProfileSection.Result(result));
                }
            }
            catch (Exception ex)
            {
                Log.Warn(ex, string.Format("Failed to check SQL Server state"));
                return(ProfileSection.Result(true));
            }
        }