Click here to Skip to main content
15,914,943 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 307.8K   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

 
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 
In your RetrieveInformation method, you write the following (bits deleted):
C#
try
{	
  if (...)
    throw new ApplicationException("No Data Returned.");
}
catch (Exception ex)
{			
  throw new ApplicationException("Cannot Locate SQL Server.");
}
finally
{
  // ...
}
There are several reasons why the code in the try block might throw an exception: the ODBC dll may be absent, one of those calls may fail, etc.

What you are doing here is throwing away all information contained in the exception, and replacing it with a generic "Cannot Locate SQL Server" message, which is not very useful at best, and misleading at worst. It doesn't tell you anything about why the call failed.

The guideline is to catch only those exceptions that you are prepared to handle. If you don't have any meaningful information to add, don't catch the exception and rethrow another. If you do rethrow another exception, then at least use the innerException parameter of the constructor to pass the information along.

Libraries and end-user applications have different rules for exception handling. In general, end-user applications should not throw exceptions, and an end-user should be protected from unnecessary technical details. When writing a library however, more often than not you should let the developer who is using your library decide what to do with the exception.

Jeffrey

Everything should be as simple as possible, but not simpler.
    -- Albert Einstein

Numerical components for C# and VB.NET
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.