detect sql server in the network

hi all,

i am currently doing a database application using vb .net 2003. I would like to add a feature in my software wherein it would search the network for active instances of SQL Server. Also, I would like it to detect if a network is present at startup. Can anyone suggest how I can do this

Thanks


Answer this question

detect sql server in the network

  • Goodwill

    Here is some sample code to detect network availability:



    using System;
    using System.Threading;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Reflection;
    using System.IO;
    using System.Security;
    using System.Security.Permissions;
    using System.Xml;
    using System.Diagnostics;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Security.Principal;
    using System.Security.Policy;
    using Microsoft.Win32;
    using System.Net.Sockets;
    using System.Net.NetworkInformation;

    public class NetInfoTest
    {
     public static void Main()
     {
      try
      {
       DumpIPAddresses();
       NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
       NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; 
       Console.ReadLine();

      }
      catch(Exception e)
      {
       Console.WriteLine(e);
      }
      finally
      {
       Console.ForegroundColor = ConsoleColor.Green;
       Console.BackgroundColor = ConsoleColor.Black;  
      }

     }

     

     public static void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
     {
      if(e.IsAvailable)
      {
       Console.ForegroundColor = ConsoleColor.Green;
       Console.WriteLine("Network Available");
      }
      else
      {
       Console.ForegroundColor = ConsoleColor.Red;
       Console.WriteLine("Network *NOT* Available");
      }
     }

     public static void OnNetworkAddressChanged(object sender, EventArgs e)
     {


      Console.ForegroundColor = ConsoleColor.Red;
      Console.BackgroundColor = ConsoleColor.Yellow;
      Console.WriteLine("Address change event at {0}", DateTime.Now.ToString());
      Console.ForegroundColor = ConsoleColor.Green;
      Console.BackgroundColor = ConsoleColor.Black;
      DumpIPAddresses();

     }

     public static void DumpIPAddresses()
     {
      
      //1. Get All Network Interfaces
      NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

      //2. Loop over the interfaces
      foreach(NetworkInterface NI in NetworkInterfaces)
      {
       //3. Select Ethernet Interface
       if(NI.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
       {
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine(Environment.NewLine);
        //4. Print  Name and description
        Console.WriteLine(NI.Name + " " + NI.Description);
        Console.WriteLine("-------------------------------------------------");

        //5. Get the IP Interface properties
        IPInterfaceProperties IPProps = NI.GetIPProperties();

        //6. Get all the Unicast Addresses
        UnicastIPAddressInformationCollection UnicastAddresses =  IPProps.UnicastAddresses;
        foreach(UnicastIPAddressInformation uaddr in UnicastAddresses)
        {
         //7. Print ONLY IPv4 Addresses
         if(uaddr.Address.AddressFamily == AddressFamily.InterNetwork)
          Console.WriteLine("\t" + uaddr.Address.ToString());
        }

       }

      }
     }

    }

     



  • tpickett

    To detect if the network is available, you can use the System.Net.NetworkInformation namespace.  I suggest posting the SQL specific part of this question to a SQL specific form.  A generic method to determine if a host is alive and reachable is to use ping. Ping is also implemented in System.Net.NetworkInformation.



  • detect sql server in the network