Get Sql instances in .Net

zequion 446 Reputation points
2024-05-23T09:46:11.3966667+00:00

How can I retrieve a DataTable from Sql Server instances in .Net? In the .Net Framework it was done this way:

    SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;  
    DataTable table = instance.GetDataSources();  
    string ServerName = Environment.MachineName;  
    foreach (DataRow row in table.Rows){  Console.WriteLine (ServerName + "\\" + row["InstanceName"].ToString());  }  

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,040 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2024-05-23T13:51:35.6066667+00:00

    Hi,@zequion.Welcome to Microsoft Q&A.

    In .NET, you could use the SqlDataSourceEnumerator class by adding the the Microsoft.Data.Sql package.

    You can right click on your project to select Manage NuGet Packages... and search Microsoft.Data.Sql on Browse page to install the Microsoft.Data.Sql package in Visual Studio.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.