3

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ted Kon
  • 87
  • 1
  • 6
  • 2
    Did you add `using System.Data.Sql;` ? [SqlDataSourceEnumerator Classe](https://learn.microsoft.com/dotnet/api/system.data.sql.sqldatasourceenumerator) –  May 17 '21 at 23:05
  • Yeah i just did , but nothing changed .. – Ted Kon May 17 '21 at 23:09
  • Is there the System.Data.dll assembly in the project's references (it is added by default in every new project Framework or Console, but can be manually removed)? [Applies to (not Core)](https://learn.microsoft.com/en-us/dotnet/api/system.data.sql.sqldatasourceenumerator?view=netframework-4.8#applies-to). –  May 17 '21 at 23:14
  • 1
    I disovered that i had no ability to reference assemblies in project explorer and i realized that the type of project i was using was probably not correct. I created a WINDOWS FORM application c# , and now with using System.Data.Sql it seems ok ... Thanks! – Ted Kon May 17 '21 at 23:16

1 Answers1

4

At this point in time the SqlDataSourceEnumerator has not been implemented for any version of .NET Core or .NET 5. This is because the .NET Framework version relies on a native C++ implementation and the .NET team will need to reimplement it as managed code to fit their design objectives. If/when it is implemented it will be in the Microsoft.Data.SqlClient package.

There's a discussion that covers this on the dotnet/SqlClient github repository:

Note that this has been known since early 2017, and the last reference to there being any resolution is from Jul 2020.

Until this is implemented you'll have to rely on .NET Framework for any application that absolutely must use SqlDataSourceEnumerator, or find an alternative method to scan for SQL servers.

Corey
  • 15,524
  • 2
  • 35
  • 68