Click here to Skip to main content
6,291,124 members and growing! (15,020 online)
Email Password   helpLost your password?
Database » Database » SQL Server     Intermediate

Enumerate SQL Server Instances in C#, Using ODBC

By Eamonn Murray

An article on enumerating SQL Server instances in C# using ODBC thus removing any dependancy on SQLDMO. Based on the C++ article by Santosh Rao.
C#, SQL.NET 1.1, Win2K, WinXP, ADO.NET, SQL 2000, VS.NET2003, DBA, Dev
Posted:18 Apr 2005
Views:99,276
Bookmarked:53 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
23 votes for this article.
Popularity: 6.18 Rating: 4.54 out of 5
2 votes, 8.7%
1
1 vote, 4.3%
2
1 vote, 4.3%
3
2 votes, 8.7%
4
17 votes, 73.9%
5

Sample Image - C#SQLInfoEnumeratorDemo.jpg

Introduction

This article describes a C# class that utilises ODBC (SQLBrowseConnect) to obtain a list of SQL Servers on a network and returns an array of instances. If an instance is supplied with a valid username/password, then a list of all the databases on the instance is returned.

Background

I needed a way to obtain a list of SQL Server instances without using SQLDMO. I found a C++ implementation of the SQLBrowseConnect by Santosh Rao. This is a C# implementation.

Using the code

In order for the code to work, ODBC must be installed on your machine. Nearly all Microsoft Operating Systems have this installed. I have only tested this on Windows 2000 and XP. In order to use this, add a reference to the SQLEnumerator.cs file. The class is in the Moletrator.SQLDocumentor namespace and is SQLInfoEnumerator. The important work is done in the RetrieveInformation method. This calls the relevant ODBC commands passing in the relevant values. The important item is the value of inputParam. If this is blank then nothing is returned. When it contains a valid driver (DRIVER=SQL SERVER for MS SQL Server), it will check for all instances of this driver on the network returning a string value which is then parsed.

If this string is expanded to include a valid SQL Server instance and a valid username/password, then a list of all the databases on the server instance is returned. If the username/password are not valid then the a list of SQL Server instances is returned:

  1. In order to get a list of SQL Server instances, create an instance of the class and call EnumerateSQLServers. The example below adds the list of SQL Servers to a list box SQLListBox.
    SQLInfoEnumerator sie = new SQLInfoEnumerator();
    SQLListBox.Items.AddRange(sie.EnumerateSQLServers());
  2. To get a list of databases on a SQL Server instance, use the code below. The SQL Server instance is the selected instance from the list box populated in sample A. The username/password are entered by the user.
    SQLInfoEnumerator sie = new SQLInfoEnumerator();
    sie.SQLServer = listboxSQLServerInstances.SelectedItem.ToString(); 
    sie.Username = textboxUserName.Text;
    sie.Password = textboxPassword.Text;
    SQLListBox.Items.AddRange(sie.EnumerateSQLServersDatabases());

The demo code contains a full GUI with an example on how to call each of the above methods.

History

18 April 2005 1:00 p.m. - Initial write.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Eamonn Murray


Member

Location: Ireland Ireland

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
AnswerFinding SQL2005/2008-servers PinmemberJonas Hammarberg3:51 16 May '09  
GeneralEven more simplified Pinmembervsoft6:16 22 Oct '08  
GeneralRe: Even more simplified PinmemberMember 438474518:51 28 Dec '08  
GeneralRe: Even more simplified PinmembervishalMGiri18:56 28 Dec '08  
GeneralGreat... Pinmemberd00_ape0:10 1 Aug '08  
GeneralLicense Pinmemberdavidm9920:39 25 Jul '08  
GeneralRe: License Pinmemberearmitage9:36 12 Dec '08  
QuestionProblem in finding datbases Pinmemberkssknov4:59 10 Apr '08  
GeneralGreat Work indeed ! Pinmemberram kumar 202121:37 12 Sep '07  
GeneralThanks. PinmemberEric Groise6:46 23 May '07  
GeneralSQL-Server 2000 Enterprise ODBC Creation error PinmemberCh. Kaleem22:02 5 Sep '06  
Generaldetecting named msde instances Pinmemberrajeshatsrin21:56 22 Aug '06  
GeneralInstances are sometimes not enumerated PinmemberWenxiang Tao8:49 25 May '06  
GeneralRe: Instances are sometimes not enumerated Pinmemberrajeshatsrin22:00 22 Aug '06  
GeneralRe: Instances are sometimes not enumerated PinmemberMast3rMind6:59 17 Jan '07  
GeneralEnumerate SQL Server Instances in C#, Using ODBC PinmemberGodfather3223:38 15 May '06  
GeneralInstances? Pinmembermirano12:25 19 Jan '06  
GeneralRe: Instances? PinmemberAlexander Stromer2:18 29 Mar '06  
JokeEXCELENT!!! PinmemberXTonos14:08 16 Jan '06  
GeneralSQLBrowseConnect PinmemberHimmett12:37 26 Sep '05  
Generalmanipulating server? PinmemberUnruled Boy18:25 19 Jun '05  
GeneralRe: manipulating server? Pinmemberdon!marco23:40 28 Jun '05  
GeneralTrusted Connections and invalid logins PinmemberGaandu15:42 9 Jun '05  
GeneralRe: Trusted Connections and invalid logins PinmemberEamonn Murray6:18 10 Jun '05  
GeneralRe: Trusted Connections and invalid logins PinmemberGaandu6:38 11 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Apr 2005
Editor: Sean Ewington
Copyright 2005 by Eamonn Murray
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project