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.
![](https://losmac.republika.pl/images/form1.jpg)
Using the code
The code is as simple as it's possible
What you need to test the code?
- Create new project (windows application),
- 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).
Dim dt As Data.DataTable = Nothing, dr As Data.DataRow = Nothing
Try
dt = Sql.SqlDataSourceEnumerator.Instance.GetDataSources()
For Each dr In dt.Rows
Me.CmbSQLInstance.Items.Add(dr.Item(0).ToString)
Next
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
dr = Nothing
dt = Nothing
End Try
Alternatives
History
- 2013/02/11 - First version.
- 2013/02/13 - source files added
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!
- C#
- SQL
- Linq
- VB/VBA/VB.NET
- XML & XSL
- Python
- JCL
If you want to contact me... find me on LinkedIn.