Ted Kon
Ted Kon

Reputation: 99

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?

Upvotes: 3

Views: 2409

Answers (2)

Larry
Larry

Reputation: 34

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

Upvotes: 2

Corey
Corey

Reputation: 16584

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.

Upvotes: 4

Related Questions