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, "Failed to check SQL Server state"); return ProfileSection.Result(true); } }
protected string GetProductName([NotNull] string instanceName) { Assert.ArgumentNotNull(instanceName, "instanceName"); string value = new SqlConnectionStringBuilder(this.Value).InitialCatalog; string[] arr = value.Split('_'); return arr.Length == 2 ? arr[0].TrimStart(instanceName) : string.Empty; }