Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server

Enumerate SQL Server Instances in C#, Using ODBC

Rate me:
Please Sign up or sign in to vote.
4.90/5 (33 votes)
18 Apr 2005CPOL2 min read 304.9K   11.7K   80   49
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.

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.
    C#
    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.
    C#
    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Ireland Ireland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWonderful! 10 out of 10! Pin
Member 1521568315-Jun-23 21:33
Member 1521568315-Jun-23 21:33 
GeneralMy vote of 5 Pin
Joseph Guenther24-Aug-21 4:40
Joseph Guenther24-Aug-21 4:40 
PraiseAWESOME!!! Pin
Joseph Guenther24-Aug-21 4:39
Joseph Guenther24-Aug-21 4:39 
GeneralMy vote of 5 Pin
tina_nbz12-Feb-13 1:47
tina_nbz12-Feb-13 1:47 
GeneralMy vote of 5 Pin
Kanasz Robert24-Sep-12 5:56
professionalKanasz Robert24-Sep-12 5:56 
GeneralMy vote of 2 Pin
Abir Stolov29-Aug-12 0:39
Abir Stolov29-Aug-12 0:39 
QuestionReading a column value in unicode in sql server side Pin
cancerion13-Jun-12 19:58
cancerion13-Jun-12 19:58 
GeneralMy vote of 2 Pin
gggustafson4-Mar-11 3:26
mvagggustafson4-Mar-11 3:26 
GeneralVery good article Pin
Yanela Somdaka12-Jan-10 22:49
Yanela Somdaka12-Jan-10 22:49 
AnswerFinding SQL2005/2008-servers Pin
Jonas Hammarberg16-May-09 2:51
professionalJonas Hammarberg16-May-09 2:51 
GeneralEven more simplified Pin
vsoft22-Oct-08 5:16
vsoft22-Oct-08 5:16 
GeneralRe: Even more simplified Pin
vishalMGiri28-Dec-08 17:51
vishalMGiri28-Dec-08 17:51 
GeneralRe: Even more simplified Pin
vishalMGiri28-Dec-08 17:56
vishalMGiri28-Dec-08 17:56 
GeneralGreat... Pin
anderslundsgard31-Jul-08 23:10
anderslundsgard31-Jul-08 23:10 
GeneralLicense Pin
davidm9925-Jul-08 19:39
davidm9925-Jul-08 19:39 
GeneralRe: License Pin
earmitage12-Dec-08 8:36
earmitage12-Dec-08 8:36 
QuestionProblem in finding datbases Pin
kssknov10-Apr-08 3:59
kssknov10-Apr-08 3:59 
GeneralGreat Work indeed ! Pin
ram kumar 202112-Sep-07 20:37
ram kumar 202112-Sep-07 20:37 
GeneralThanks. Pin
Eric Groise23-May-07 5:46
Eric Groise23-May-07 5:46 
Generaldetecting named msde instances Pin
rajeshatsrin22-Aug-06 20:56
rajeshatsrin22-Aug-06 20:56 
GeneralInstances are sometimes not enumerated Pin
Wenxiang Tao25-May-06 7:49
Wenxiang Tao25-May-06 7:49 
GeneralRe: Instances are sometimes not enumerated Pin
rajeshatsrin22-Aug-06 21:00
rajeshatsrin22-Aug-06 21:00 
GeneralRe: Instances are sometimes not enumerated Pin
Mast3rMind17-Jan-07 5:59
Mast3rMind17-Jan-07 5:59 
GeneralEnumerate SQL Server Instances in C#, Using ODBC Pin
Godfather3215-May-06 22:38
Godfather3215-May-06 22:38 
QuestionInstances? Pin
mirano19-Jan-06 11:25
mirano19-Jan-06 11:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.