Click here to Skip to main content
15,879,184 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 305.4K   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

 
AnswerRe: Instances? Pin
Alexander Stromer29-Mar-06 1:18
Alexander Stromer29-Mar-06 1:18 
JokeEXCELENT!!! Pin
XTonos16-Jan-06 13:08
XTonos16-Jan-06 13:08 
GeneralSQLBrowseConnect Pin
Himmett26-Sep-05 11:37
Himmett26-Sep-05 11:37 
Questionmanipulating server? Pin
Huisheng Chen19-Jun-05 17:25
Huisheng Chen19-Jun-05 17:25 
AnswerRe: manipulating server? Pin
don!marco28-Jun-05 22:40
don!marco28-Jun-05 22:40 
GeneralTrusted Connections and invalid logins Pin
Gaandu9-Jun-05 14:42
Gaandu9-Jun-05 14:42 
GeneralRe: Trusted Connections and invalid logins Pin
Eamonn Murray10-Jun-05 5:18
Eamonn Murray10-Jun-05 5:18 
GeneralRe: Trusted Connections and invalid logins Pin
Gaandu11-Jun-05 5:38
Gaandu11-Jun-05 5:38 
GeneralRe: Trusted Connections and invalid logins Pin
gadzin17-Aug-05 5:26
gadzin17-Aug-05 5:26 
GeneralRe: Trusted Connections and invalid logins Pin
JSBarten4-Nov-05 7:09
JSBarten4-Nov-05 7:09 
GeneralRe: Trusted Connections and invalid logins [modified] Pin
DelboyDee10-May-07 0:25
DelboyDee10-May-07 0:25 
QuestionRe: Trusted Connections and invalid logins Pin
lildiapaz5-Jul-07 5:02
lildiapaz5-Jul-07 5:02 
GeneralSomething is not right here Pin
Tom Wright8-Jun-05 12:41
Tom Wright8-Jun-05 12:41 
GeneralRe: Something is not right here Pin
Eamonn Murray9-Jun-05 4:53
Eamonn Murray9-Jun-05 4:53 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 0:12
TechnicalAli24-Aug-05 0:12 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 1:33
TechnicalAli24-Aug-05 1:33 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 4:38
TechnicalAli24-Aug-05 4:38 
GeneralRe: Something is not right here Pin
mqmin19-Jul-07 4:51
mqmin19-Jul-07 4:51 
QuestionCan you help please? Pin
Tom Wright7-Jun-05 10:41
Tom Wright7-Jun-05 10:41 
AnswerRe: Can you help please? Pin
Anonymous8-Jun-05 9:01
Anonymous8-Jun-05 9:01 
GeneralChatSet.Ansi error Pin
nickshengfei26-Apr-05 12:54
nickshengfei26-Apr-05 12:54 
GeneralRe: ChatSet.Ansi error Pin
Eamonn Murray11-May-05 0:10
Eamonn Murray11-May-05 0:10 
GeneralException Handling Pin
Jeffrey Sax21-Apr-05 2:27
Jeffrey Sax21-Apr-05 2:27 
GeneralEverybody - pls read the above! Pin
Anonymous27-Apr-05 19:24
Anonymous27-Apr-05 19:24 

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.