Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to find all the MSSQL data servers on my network.

Below is the code:

Option Strict Off
Option Explicit On
Imports System.Data.Common
Imports System.Data.Sql

Public Class Form1

#Region "Class Header"
    Inherits Form

#End Region

#Region "Start-up"

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
    End Sub

#End Region

#Region "Commands"

    Dim mDT As DataTable ' the data table that will be retrieved from GetDataSources call
    Dim mSqlEnum As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance ' the MSSQL enumerator instance

    Private Sub btnGetDBList_Click(sender As Object, e As EventArgs) Handles btnGetDBList.Click
        mDT = mSqlEnum.GetDataSources
        Dim tstr As String = "Number of MSSQL Servers found = " & mDT.Rows.Count
        MsgBox(tstr)
    End Sub
#End Region

End Class


The result of the
btnGetDBList_Click
method reports zero data servers found. It does not even find the data server running on my machine.

What I have tried:

Reviewed numerous examples found on the web. The above code seems to be correct.
Posted
Updated 5-Apr-20 9:03am

It works fine for me:
VB
Dim sdse As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
Dim dt As DataTable = sdse.GetDataSources()

For Each row As DataRow In dt.Rows

    For Each col As DataColumn In dt.Columns
        Console.Write(row(col))
        Console.Write(", ")
    Next

    Console.WriteLine()
Next

Console.WriteLine(dt.Rows.Count)
Finds my local SQL Server instance without any problems:
GRDESK, SQLEXPRESS, No, 11.0.5058.0, 
1
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900