public static void RetrieveMachineSqlInstance(ref IList <SqlInstance> xioResult) { try { Logger.Instance.LogTrace(@"Smo: Looking for machine specific instances..."); RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"); string [] instances = (string[])rk.GetValue("InstalledInstances"); foreach (string instance in instances) { SqlInstance sqlInstanceInfo = new SqlInstance(); if (instance.CompareTo("MSSQLSERVER") == 0 || instance.StartsWith("MSSQL$")) { sqlInstanceInfo.Name = Environment.MachineName; } else { sqlInstanceInfo.Name = Environment.MachineName + Path.DirectorySeparatorChar + instance; } sqlInstanceInfo.Instance = sqlInstanceInfo.Name; sqlInstanceInfo.IsClustered = false; sqlInstanceInfo.IsLocal = true; sqlInstanceInfo.IsDefault = true; sqlInstanceInfo.Server = Environment.MachineName; sqlInstanceInfo.Version = string.Empty; try { RegistryKey instanceRk = rk.OpenSubKey(instance + @"\MSSQLServer\CurrentVersion"); if (instanceRk != null) { sqlInstanceInfo.Version = (string)instanceRk.GetValue(@"CurrentVersion"); } } catch { } Logger.Instance.LogTrace("Smo: {0}.", sqlInstanceInfo.ToString()); xioResult.Add(sqlInstanceInfo); } Logger.Instance.LogTrace(@"Smo: Finished enumeration of machine specific instances."); } catch (Exception ex) { Logger.Instance.LogException(ex); } }
public Smo() { AttachedDatabaseName = string.Empty; RemoteFolder = string.Empty; IsCurrentInstanceReachable = true; IsMountPointAlive = true; DisposedSyncRoot = new object(); CurrentInstance = new SqlInstance(); m_Timer = new System.Timers.Timer(TimeSpan.FromSeconds(kPingDelay).TotalMilliseconds); m_Timer.Elapsed += (object xiSender, ElapsedEventArgs xiEventArgs) => { if (!m_TimerCallbackInProgress) { m_TimerCallbackInProgress = true; try { PingRemoteFolder(); PingDatabase(); } catch (Exception ex) { Logger.Instance.LogException(ex, "Smo: Ping failed in timer callback."); } finally { m_TimerCallbackInProgress = false; } } }; m_Timer.Enabled = false; }
private static void PopulateSqlInstancesResults(DataTable xiSqlServers, ref IList <SqlInstance> xioResult) { foreach (DataRow dataRow in xiSqlServers.Rows) { #if DETAILED_LOGING Logger.Instance.LogTrace(@"Starting retriving fields..."); #endif SqlInstance sqlInstanceInfo = new SqlInstance(); #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving Name..."); #endif if (dataRow.Table.Columns.Contains("Name") && !Convert.IsDBNull(dataRow["Name"])) { sqlInstanceInfo.Name = (string)dataRow["Name"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Name."); #endif } #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving Server..."); #endif if (dataRow.Table.Columns.Contains("Server") && !Convert.IsDBNull(dataRow["Server"])) { sqlInstanceInfo.Server = (string)dataRow["Server"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Server."); #endif } if (dataRow.Table.Columns.Contains("ServerName") && !Convert.IsDBNull(dataRow["ServerName"])) { sqlInstanceInfo.Server = (string)dataRow["ServerName"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Server."); #endif } #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving Instance..."); #endif if (dataRow.Table.Columns.Contains("Instance") && !Convert.IsDBNull(dataRow["Instance"])) { sqlInstanceInfo.Instance = (string)dataRow["Instance"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Instance."); #endif } if (dataRow.Table.Columns.Contains("InstanceName") && !Convert.IsDBNull(dataRow["InstanceName"])) { sqlInstanceInfo.Instance = (string)dataRow["InstanceName"]; sqlInstanceInfo.Name = sqlInstanceInfo.Server + @"\" + sqlInstanceInfo.Instance; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Instance."); #endif } #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving IsClustered..."); #endif if (dataRow.Table.Columns.Contains("IsClustered") && !Convert.IsDBNull(dataRow["IsClustered"])) { sqlInstanceInfo.IsClustered = (dataRow["IsClustered"] is bool) ? (bool)dataRow["IsClustered"] : ((dataRow["IsClustered"] as string).ToLower().CompareTo("yes") == 0 || (dataRow["IsClustered"] as string).ToLower().CompareTo("true") == 0); #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved IsClustered."); #endif } #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving Version..."); #endif if (dataRow.Table.Columns.Contains("Version") && !Convert.IsDBNull(dataRow["Version"])) { sqlInstanceInfo.Version = (string)dataRow["Version"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved Version."); #endif } #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieving IsLocal..."); #endif if (dataRow.Table.Columns.Contains("IsLocal") && !Convert.IsDBNull(dataRow["IsLocal"])) { sqlInstanceInfo.IsLocal = (bool)dataRow["IsLocal"]; #if DETAILED_LOGING Logger.Instance.LogTrace(@"Retrieved IsLocal."); #endif } Logger.Instance.LogTrace("Smo: {0}.", sqlInstanceInfo.ToString()); if (!string.IsNullOrEmpty(sqlInstanceInfo.Name)) { bool exists = false; foreach (SqlInstance instance in xioResult) { if (instance.Name.CompareTo(sqlInstanceInfo.Name) == 0) { exists = true; break; } } if (!exists) { xioResult.Add(sqlInstanceInfo); } } } }