Search code examples
c#sqldatasource

The name 'SqlDataSourceEnumerator' does not exist in the current context


In a C# .net 5 program in Visual Studio I am testing a code that is using SqlDataSourceEnumerator

public static List<SqlServerInstance> LocateSqlInstances()
{
    List<SqlServerInstance> results = new List<SqlServerInstance>();

    using (DataTable sqlSources = SqlDataSourceEnumerator.Instance.GetDataSources())
    {
        foreach (DataRow source in sqlSources.Rows)
        {
            string servername;
            string instancename = source["InstanceName"].ToString();

            if (!string.IsNullOrEmpty(instancename))
            {
                servername = source["ServerName"].ToString() + '\\' + instancename;
            }
            else
            {
                servername = source["ServerName"].ToString();
            }

            results.Add(new SqlServerInstance() { ServerInstance = servername, Version = source["Version"].ToString() });
        }
    }

    return results;
}

Although I have using System.Data; I get

Error CS0103 The name 'SqlDataSourceEnumerator' does not exist in the current context

Why is this happening?


Solution

  • You will need to add the nuget package of Microsoft.Data.Sql