Ejemplo n.º 1
0
        public static List <SqlServerInstance> EnumLocalInstances()
        {
            var retVal = new List <SqlServerInstance>();

            try
            {
                var registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
                using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView))
                {
                    var instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
                    if (instanceKey != null)
                    {
                        retVal.AddRange(instanceKey.GetValueNames().Select(SqlServerInstance.CreateLocal));
                    }
                }
            }
            catch (Exception ex)
            {
                LogWrapper.WriteError(ex.ToString());
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public static List <SqlServerInstance> EnumRemoteInstances()
        {
            var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
            {
                EnableBroadcast = true,
                ReceiveTimeout  = 1000,
                DualMode        = true
            };

            var bytes = new List <byte>(4096);

            try
            {
                var    ipv4ep = new IPEndPoint(IPAddress.Broadcast, 1434);
                var    ipv6ep = new IPEndPoint(IPAddress.Parse("ff02::1"), 1434);
                byte[] msg    = { 0x02 };
                socket.SendTo(msg, ipv4ep);
                socket.SendTo(msg, ipv6ep);

                int    cnt        = 0;
                byte[] byteBuffer = new byte[4096];

                do
                {
                    cnt = socket.Receive(byteBuffer);

                    bytes.AddRange(byteBuffer.Take(cnt));
                } while (cnt != 0);
            }
            catch (SocketException sex)
            {
                const int WSAETIMEDOUT    = 10060;              // Connection timed out.
                const int WSAEHOSTUNREACH = 10065;              // No route to host.

                if (sex.ErrorCode == WSAETIMEDOUT || sex.ErrorCode == WSAEHOSTUNREACH)
                {
                }
                else
                {
                    LogWrapper.WriteError(sex.ToString());
                    return(new List <SqlServerInstance>());
                }
            }
            finally
            {
                socket.Close();
            }

            string text = Encoding.ASCII.GetString(bytes.ToArray());

            var servers = text.Split(new[] { ";;" }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Substring(s.IndexOf("ServerName", StringComparison.Ordinal)));

            return(servers
                   .Select(server => PatternRegex.Match(server))
                   .Select(match => SqlServerInstance.Create(match.Groups["server"].Value, match.Groups["instance"].Value))
                   .Distinct(SqlServerInstance.Comparer)
                   .Where(instance => !instance.IsLocal)
                   .OrderBy(instance => instance.ServerName)
                   .ThenBy(instance => instance.InstanceName)
                   .ToList());
        }