Click here to Skip to main content
Click here to Skip to main content

Enumerate SQL Server Instances in C#, Using ODBC

By , 18 Apr 2005
 

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

About the Author

earmitage
Web Developer
Ireland Ireland
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membertina_nbz12 Feb '13 - 1:47 
GeneralMy vote of 5mvpKanasz Robert24 Sep '12 - 5:56 
GeneralMy vote of 2memberAbir Stolov29 Aug '12 - 0:39 
QuestionReading a column value in unicode in sql server sidemembercancerion13 Jun '12 - 19:58 
GeneralMy vote of 2membergggustafson4 Mar '11 - 3:26 
GeneralVery good articlememberYanela Somdaka12 Jan '10 - 22:49 
AnswerFinding SQL2005/2008-serversmemberJonas Hammarberg16 May '09 - 2:51 
GeneralEven more simplifiedmembervsoft22 Oct '08 - 5:16 
Enumerate instances:
DataTable table = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();
 
Dump into XML:
table.WriteXml("sql-servers.xml");

 
Vasek

GeneralRe: Even more simplifiedmemberMember 438474528 Dec '08 - 17:51 
GeneralRe: Even more simplifiedmembervishalMGiri28 Dec '08 - 17:56 
GeneralGreat...memberd00_ape31 Jul '08 - 23:10 
GeneralLicensememberdavidm9925 Jul '08 - 19:39 
GeneralRe: Licensememberearmitage12 Dec '08 - 8:36 
QuestionProblem in finding datbasesmemberkssknov10 Apr '08 - 3:59 
GeneralGreat Work indeed !memberram kumar 202112 Sep '07 - 20:37 
GeneralThanks.memberEric Groise23 May '07 - 5:46 
GeneralSQL-Server 2000 Enterprise ODBC Creation errormemberCh. Kaleem5 Sep '06 - 21:02 
Generaldetecting named msde instancesmemberrajeshatsrin22 Aug '06 - 20:56 
GeneralInstances are sometimes not enumeratedmemberWenxiang Tao25 May '06 - 7:49 
GeneralRe: Instances are sometimes not enumeratedmemberrajeshatsrin22 Aug '06 - 21:00 
GeneralRe: Instances are sometimes not enumeratedmemberMast3rMind17 Jan '07 - 5:59 
GeneralEnumerate SQL Server Instances in C#, Using ODBCmemberGodfather3215 May '06 - 22:38 
QuestionInstances?membermirano19 Jan '06 - 11:25 
AnswerRe: Instances?memberAlexander Stromer29 Mar '06 - 1:18 
JokeEXCELENT!!!memberXTonos16 Jan '06 - 13:08 
GeneralSQLBrowseConnectmemberHimmett26 Sep '05 - 11:37 
Questionmanipulating server?memberUnruled Boy19 Jun '05 - 17:25 
AnswerRe: manipulating server?memberdon!marco28 Jun '05 - 22:40 
GeneralTrusted Connections and invalid loginsmemberGaandu9 Jun '05 - 14:42 
GeneralRe: Trusted Connections and invalid loginsmemberEamonn Murray10 Jun '05 - 5:18 
GeneralRe: Trusted Connections and invalid loginsmemberGaandu11 Jun '05 - 5:38 
GeneralRe: Trusted Connections and invalid loginsmembergadzin17 Aug '05 - 5:26 
GeneralRe: Trusted Connections and invalid loginsmemberJSBarten4 Nov '05 - 7:09 
GeneralRe: Trusted Connections and invalid logins [modified]memberDelboyDee10 May '07 - 0:25 
QuestionRe: Trusted Connections and invalid loginsmemberlildiapaz5 Jul '07 - 5:02 
GeneralSomething is not right herememberTom Wright8 Jun '05 - 12:41 
GeneralRe: Something is not right herememberEamonn Murray9 Jun '05 - 4:53 
GeneralRe: Something is not right herememberTechnicalAli24 Aug '05 - 0:12 
GeneralRe: Something is not right herememberTechnicalAli24 Aug '05 - 1:33 
GeneralRe: Something is not right herememberTechnicalAli24 Aug '05 - 4:38 
GeneralRe: Something is not right heremembermqmin19 Jul '07 - 4:51 
QuestionCan you help please?memberTom Wright7 Jun '05 - 10:41 
AnswerRe: Can you help please?sussAnonymous8 Jun '05 - 9:01 
GeneralChatSet.Ansi errormembernickshengfei26 Apr '05 - 12:54 
GeneralRe: ChatSet.Ansi errormemberEamonn Murray11 May '05 - 0:10 
GeneralException HandlingmemberJeffrey Sax21 Apr '05 - 2:27 
GeneralEverybody - pls read the above!sussAnonymous27 Apr '05 - 19:24 

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

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