Click here to Skip to main content
12,633,336 members (29,792 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked

Enumerate SQL Server Instances in C#, Using ODBC

, 18 Apr 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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


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.


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();
  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;

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


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Ireland Ireland
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
tina_nbz12-Feb-13 2:47
membertina_nbz12-Feb-13 2:47 
GeneralMy vote of 5 Pin
Kanasz Robert24-Sep-12 6:56
mvpKanasz Robert24-Sep-12 6:56 
GeneralMy vote of 2 Pin
Abir Stolov29-Aug-12 1:39
memberAbir Stolov29-Aug-12 1:39 
QuestionReading a column value in unicode in sql server side Pin
cancerion13-Jun-12 20:58
membercancerion13-Jun-12 20:58 

In our SQL Server 2008 database in extended property collation is set as SQL_Latin1_General_CP1_CI_AS'.
I have a table, which contains string ported from text file having ASCII, UTF-8 formatted strings. Our requirement is that, we need to read value as Unicode from this table and then process that and fill other tables. We are not sure whether data present in ASCII, UNICODE or UTF-8/16.

That' why we are looking for a procedure which can convert string to Unicode irrespective of encoding scheme.

Suppose in a table i have data is utf-8 format for e.g.

Carrer de Béjar

Calle de Antonio Machado

Calle del Canónigo Domenech

Carrer de Pau Arpí i Galí

Calle del Abad Oliba

I want them to read in Unicode format, I have tried using Collate but could not succeed.
Please share your solution.


GeneralMy vote of 2 Pin
gggustafson4-Mar-11 4:26
membergggustafson4-Mar-11 4:26 
GeneralVery good article Pin
Yanela Somdaka12-Jan-10 23:49
memberYanela Somdaka12-Jan-10 23:49 
AnswerFinding SQL2005/2008-servers Pin
Jonas Hammarberg16-May-09 3:51
memberJonas Hammarberg16-May-09 3:51 
GeneralEven more simplified Pin
vsoft22-Oct-08 6:16
membervsoft22-Oct-08 6:16 
GeneralRe: Even more simplified Pin
Member 438474528-Dec-08 18:51
memberMember 438474528-Dec-08 18:51 
GeneralRe: Even more simplified Pin
vishalMGiri28-Dec-08 18:56
membervishalMGiri28-Dec-08 18:56 
GeneralGreat... Pin
d00_ape1-Aug-08 0:10
memberd00_ape1-Aug-08 0:10 
GeneralLicense Pin
davidm9925-Jul-08 20:39
memberdavidm9925-Jul-08 20:39 
GeneralRe: License Pin
earmitage12-Dec-08 9:36
memberearmitage12-Dec-08 9:36 
QuestionProblem in finding datbases Pin
kssknov10-Apr-08 4:59
memberkssknov10-Apr-08 4:59 
GeneralGreat Work indeed ! Pin
ram kumar 202112-Sep-07 21:37
memberram kumar 202112-Sep-07 21:37 
GeneralThanks. Pin
Eric Groise23-May-07 6:46
memberEric Groise23-May-07 6:46 
GeneralMessage Removed Pin
Ch. Kaleem5-Sep-06 22:02
memberCh. Kaleem5-Sep-06 22:02 
Generaldetecting named msde instances Pin
rajeshatsrin22-Aug-06 21:56
memberrajeshatsrin22-Aug-06 21:56 
GeneralInstances are sometimes not enumerated Pin
Wenxiang Tao25-May-06 8:49
memberWenxiang Tao25-May-06 8:49 
GeneralRe: Instances are sometimes not enumerated Pin
rajeshatsrin22-Aug-06 22:00
memberrajeshatsrin22-Aug-06 22:00 
GeneralRe: Instances are sometimes not enumerated Pin
Mast3rMind17-Jan-07 6:59
memberMast3rMind17-Jan-07 6:59 
GeneralEnumerate SQL Server Instances in C#, Using ODBC Pin
Godfather3215-May-06 23:38
memberGodfather3215-May-06 23:38 
QuestionInstances? Pin
mirano19-Jan-06 12:25
membermirano19-Jan-06 12:25 
AnswerRe: Instances? Pin
Alexander Stromer29-Mar-06 2:18
memberAlexander Stromer29-Mar-06 2:18 
XTonos16-Jan-06 14:08
memberXTonos16-Jan-06 14:08 
GeneralSQLBrowseConnect Pin
Himmett26-Sep-05 12:37
memberHimmett26-Sep-05 12:37 
Questionmanipulating server? Pin
Unruled Boy19-Jun-05 18:25
memberUnruled Boy19-Jun-05 18:25 
AnswerRe: manipulating server? Pin
don!marco28-Jun-05 23:40
memberdon!marco28-Jun-05 23:40 
GeneralTrusted Connections and invalid logins Pin
Gaandu9-Jun-05 15:42
memberGaandu9-Jun-05 15:42 
GeneralRe: Trusted Connections and invalid logins Pin
Eamonn Murray10-Jun-05 6:18
memberEamonn Murray10-Jun-05 6:18 
GeneralRe: Trusted Connections and invalid logins Pin
Gaandu11-Jun-05 6:38
memberGaandu11-Jun-05 6:38 
GeneralRe: Trusted Connections and invalid logins Pin
gadzin17-Aug-05 6:26
membergadzin17-Aug-05 6:26 
GeneralRe: Trusted Connections and invalid logins Pin
JSBarten4-Nov-05 8:09
memberJSBarten4-Nov-05 8:09 
GeneralRe: Trusted Connections and invalid logins [modified] Pin
DelboyDee10-May-07 1:25
memberDelboyDee10-May-07 1:25 
QuestionRe: Trusted Connections and invalid logins Pin
lildiapaz5-Jul-07 6:02
memberlildiapaz5-Jul-07 6:02 
GeneralSomething is not right here Pin
Tom Wright8-Jun-05 13:41
memberTom Wright8-Jun-05 13:41 
GeneralRe: Something is not right here Pin
Eamonn Murray9-Jun-05 5:53
memberEamonn Murray9-Jun-05 5:53 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 1:12
memberTechnicalAli24-Aug-05 1:12 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 2:33
memberTechnicalAli24-Aug-05 2:33 
GeneralRe: Something is not right here Pin
TechnicalAli24-Aug-05 5:38
memberTechnicalAli24-Aug-05 5:38 
GeneralRe: Something is not right here Pin
mqmin19-Jul-07 5:51
membermqmin19-Jul-07 5:51 
QuestionCan you help please? Pin
Tom Wright7-Jun-05 11:41
memberTom Wright7-Jun-05 11:41 
AnswerRe: Can you help please? Pin
Anonymous8-Jun-05 10:01
sussAnonymous8-Jun-05 10:01 
GeneralChatSet.Ansi error Pin
nickshengfei26-Apr-05 13:54
membernickshengfei26-Apr-05 13:54 
GeneralRe: ChatSet.Ansi error Pin
Eamonn Murray11-May-05 1:10
memberEamonn Murray11-May-05 1:10 
GeneralException Handling Pin
Jeffrey Sax21-Apr-05 3:27
memberJeffrey Sax21-Apr-05 3:27 
GeneralEverybody - pls read the above! Pin
Anonymous27-Apr-05 20:24
sussAnonymous27-Apr-05 20: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 19 Apr 2005
Article Copyright 2005 by earmitage
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid