Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server / SQL-server-2000

Locate SQL Server Instances on the Local Network

0.00/5 (No votes)
30 Apr 2012CPOL 9.4K  
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.

C++
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.

C#
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)