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

How to enum SQL Server instances in network

, 13 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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).

'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!
  1. VB/VBA/VB.NET
  2. C#
  3. C++
  4. SQL
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmvpSergey Alexandrovich Kryukov13-Aug-14 6:22 
GeneralRe: My vote of 5 PinmvpMaciej Los13-Aug-14 10:25 
QuestionEnum also Available Databases PinmemberMember 1094242312-Jul-14 13:17 
AnswerRe: Enum also Available Databases PinmvpMaciej Los12-Jul-14 22:17 
GeneralMy vote of 5 PinmemberAdarsh chauhan14-Aug-13 2:12 
GeneralRe: My vote of 5 PinmvpMaciej Los14-Aug-13 2:19 
QuestionNice.. PinprofessionalRaja Sekhar S1-Aug-13 1:47 
AnswerRe: Nice.. PinmvpMaciej Los1-Aug-13 1:49 
GeneralMy vote of 5 PinmemberVitorHugoGarcia26-Feb-13 4:56 
AnswerRe: My vote of 5 PinmvpMaciej Los2-Mar-13 1:10 
GeneralRe: My vote of 5 PinmemberVitorHugoGarcia3-Mar-13 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.141015.1 | Last Updated 13 Feb 2013
Article Copyright 2013 by Maciej Los
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid