Most likely your network no longer has the SQLBrowser service setup. Security usually shuts this down.
List SQL Server Instances

Good afternoon,
I have a windows form that I am working with. When I first wrote this code it worked and was able to pull a list of SQL Servers for a ComboBox, and then as I changed servers it would populate another ComboBox with databases. I had not updated the code in a few years, and it is using .net 4.6.1 and the Microsoft.SqlServer.SqlManagementObjects version: 150.18147.0. I went to use the program and launched it now it is not returning any sql servers. I opened up visual studio and went to debug it and it is still not returning servers.
All this code is contained within the form class itself.
private async void ActiveStatuses_LoadAsync(object sender, EventArgs e)
{
//List all the servers, if only want to see local set to true
DataTable dataTable = await System.Threading.Tasks.Task.Run(() => { return SmoApplication.EnumAvailableSqlServers(false); });
cbServers.ValueMember = "Name";
cbServers.DataSource = dataTable;
}
private void cbServers_SelectedIndexChanged(object sender, EventArgs e)
{
cbDatabases.Items.Clear();
if (cbServers.SelectedIndex != -1)
{
string serverName = cbServers.SelectedValue.ToString();
Server server = new Server(serverName);
try
{
foreach (Database database in server.Databases)
{
cbDatabases.Items.Add(database.Name);
}
}
catch (Exception ex)
{
string excepection = ex.Message;
}
}
}
private void cbDatabases_SelectedIndexChanged(object sender, EventArgs e)
{
string databaseName = cbDatabases.SelectedItem.ToString();
}
I cannot see why this would stop working and debugging shows enumeration yielded no results. Also it does output any errors.
4 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 31,496 Reputation points
2022-08-18T20:06:42.967+00:00 Rijwan Ansari 711 Reputation points MVP2022-08-19T13:00:32.85+00:00 Hi
Can you try the below code?
//// Retrieve the enumerator instance, and then retrieve the data sources. SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance; DataTable dtDatabaseSources = instance.GetDataSources();
Sure registry would help if this was only getting local instances. Need to be able to poll and retrieve network instances from the servers, the local machine, and other machines in the office. Sometimes I am asked to help pull data with this program.
Here are the references that are in use:
Microsoft.CSharp
Microsoft.SqlServer.Assessmetn
Microsoft.SqlServer.BatchParserClient
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.ConnectionInfoExtended
Microsoft.SqlServer.Diagnostics.Strace
Microsoft.SqlServer.Dmf
Microsoft.SqlServer.Dmf.Common
Microsoft.SQLServer.ManagedDTS
Microsoft.SqlServer.Management.Assessment
Microsoft.SqlServer.Management.Collector
Microsoft.SqlServer.Management.CollectorEnum
Microsoft.SqlServer.Management.RegisteredServers
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Management.SqlParser
Microsoft.SqlServer.Management.Utility
Microsoft.SqlServer.Management.UtilityEnum
Microsoft.SqlServer.Management.XEvent
Microsoft.SqlServer.Management.XEventDbScoped
Microsoft.SqlServer.Management.XEventDbScopedEnum
Microsoft.SqlServer.PolicyEnum
Microsoft.SqlServer.RegSvrEnum
Microsoft.SqlServer.ServiceBrokerEnum
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoExtended
Microsoft.SqlServer.SqlClrProvider
Microsoft.SqlServer.SqlEnum
Microsoft.SqlServer.SqlTDiagm
Microsoft.SqlServer.SqlWmiManagement
Microsoft.SqlServer.SString
Microsoft.SqlServer.Types
Microsoft.SqlServer.WmiEnum
References continued:
System
System.Core
System.Data
System.Data.DataSetExtensions
System.Deployment
System.Drawing
System.IdentityModel
System.Net.Http
System.Windows.Forms
System.Xml
System.Xml.Linq
using System;
using System.Data;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.Win32;