Click here to Skip to main content
14,929,655 members
Articles / Database Development / SQL Server
Tip/Trick
Posted 11 Feb 2013

Stats

50.8K views
2.4K downloads
13 bookmarked

How to enum SQL Server instances in network

Rate me:
Please Sign up or sign in to vote.
4.96/5 (9 votes)
13 Feb 2013CPOL
This tip is desired for those who want to list all available SQl Server instances that are in a network.

Introduction

Sometimes we need to list all available SQL Server instances in LAN to gain information about them. For example, it's useful when we need to write a custom installer for our database.

This tip shows us how to solves this problem. 

Using the code  

The code is as simple as it's possible Wink | ;)

What you need to test the code? 

  1. Create new project (windows application), 
  2. Add: 
    • 1 Label
    • 1 ComboBox (change it name to: CmbSQLInstance
    • 1 DataGridView  (change it name to: DGVSQLInstances)

Before you start coding, you need to set reference to  System.Data.Sql namespace (as is described here: http://msdn.microsoft.com/en-us/library/vstudio/wkze6zky%28v=vs.80%29.aspx).

VB
'declare variables
Dim dt As Data.DataTable = Nothing, dr As Data.DataRow = Nothing

Try
    'get sql server instances in to DataTable object
    dt = Sql.SqlDataSourceEnumerator.Instance.GetDataSources()

    'load data in to ComboBox
    For Each dr In dt.Rows
        Me.CmbSQLInstance.Items.Add(dr.Item(0).ToString)
    Next
    'load data in to DataGridView
    Me.DGVSQLInstances.DataSource = dt

Catch ex As System.Data.SqlClient.SqlException
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")

Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")

Finally
    'clean up ;)
    dr = Nothing
    dt = Nothing
End Try 

Alternatives

History

  • 2013/02/11 - First version.
  • 2013/02/13 - source files added

License

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

Share

About the Author

Maciej Los
Team Leader
Poland Poland
I'm a lawyer, teacher/trainer and author of VBA programming book (only polish version). Programming is my hobby and the source of extra earnings!
  1. C#
  2. SQL
  3. Linq
  4. VB/VBA/VB.NET
  5. XML & XSL


If you want to contact me... find me on LinkedIn.

Comments and Discussions

 
QuestionNot guaranteed to work always Pin
rajeshrdas20083-Mar-16 18:25
Memberrajeshrdas20083-Mar-16 18:25 
AnswerRe: Not guaranteed to work always Pin
Maciej Los3-Mar-16 19:59
mveMaciej Los3-Mar-16 19:59 
GeneralMy vote of 5 Pin
Sergey Alexandrovich Kryukov13-Aug-14 6:22
mvaSergey Alexandrovich Kryukov13-Aug-14 6:22 
GeneralRe: My vote of 5 Pin
Maciej Los13-Aug-14 10:25
mveMaciej Los13-Aug-14 10:25 
QuestionEnum also Available Databases Pin
Member 1094242312-Jul-14 13:17
MemberMember 1094242312-Jul-14 13:17 
AnswerRe: Enum also Available Databases Pin
Maciej Los12-Jul-14 22:17
mveMaciej Los12-Jul-14 22:17 
GeneralMy vote of 5 Pin
Adarsh chauhan14-Aug-13 2:12
professionalAdarsh chauhan14-Aug-13 2:12 
GeneralRe: My vote of 5 Pin
Maciej Los14-Aug-13 2:19
mveMaciej Los14-Aug-13 2:19 
QuestionNice.. Pin
Raja Sekhar S1-Aug-13 1:47
MemberRaja Sekhar S1-Aug-13 1:47 
AnswerRe: Nice.. Pin
Maciej Los1-Aug-13 1:49
mveMaciej Los1-Aug-13 1:49 
GeneralMy vote of 5 Pin
_Vitor Garcia_26-Feb-13 4:56
Member_Vitor Garcia_26-Feb-13 4:56 
AnswerRe: My vote of 5 Pin
Maciej Los2-Mar-13 1:10
mveMaciej Los2-Mar-13 1:10 
GeneralRe: My vote of 5 Pin
_Vitor Garcia_3-Mar-13 22:37
Member_Vitor Garcia_3-Mar-13 22:37 

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.