Click here to Skip to main content
Click here to Skip to main content

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"

Introduction

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[^]

Background

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 SqlServerInfo. It has one static method, Seek(), which gets the information about the SQL Server instances on the network, and returns an array of SqlServerInfo 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 "8.0.xxx" for SQL Server 2000. And TcpPort will typically be 1433.

History

        13-Dec 2005 - v 1.0 Initial release.

License

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
20+ years as a developer : Assembly, C, C++ and C#, Fortran, TSQL, ASP.Net, ActionScript,..
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by Constantin Chumak
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid