Click here to Skip to main content
13,147,689 members (29,453 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 30 Apr 2012

Locate SQL Server Instances on the Local Network

, 30 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Locate SQL Server instances on the local network"


This articles shows how to retrieve a list of PCs on the local network which are running MS SQL Server, and gets information about the instances, such as server name, instance name, version, and databases.

This article is an alternative to the article: Locate SQL Server instances on the local network[^]


To get a list of the nearby SQL Servers is as simple as typing "osql /L" on a command prompt. But getting them programmatically, for use inside an application, is a bit trickier. The most common use for this would be to create a connection string building form in managed code, which will be the subject of my next article.

Socket^ socket = gcnew Socket(AddressFamily::InterNetwork, SocketType::Dgram, ProtocolType::Udp);

ArrayList^ servers = gcnew ArrayList();
array<Byte>^ msg= {0x02};
IPEndPoint^ ep = gcnew IPEndPoint(IPAddress::Broadcast, 1434);
socket->SendTo( msg, SocketFlags::None, ep );

Using the Code

Everything is packaged in the class <span class="search-highlight">SqlServerInfo</span>. It has one static method, Seek(), which gets the information about the SQL Server instances on the network, and returns an array of <span class="search-highlight">SqlServerInfo</span> objects.

int main(array<System::String ^> ^args)
  array<SqlServerInfo^>^ lst = SqlServerInfo::Seek();

  for(int i = 0; i < lst->Length; i++)

   SqlServerInfo^ name_srv = lst[i];
    Console::WriteLine( name_srv );
  return 0;
InstanceName is typically "MSSQLSERVER", which is the default if it isn't given a specific name at installation. Version should be "" for SQL Server 2000. And TcpPort will typically be 1433.


        13-Dec 2005 - v 1.0 Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Constantin Chumak
Software Developer
Canada Canada
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by Constantin Chumak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid