Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a list of SQL Servers available on my network?

I'm trying this but I'm not sure how to proceed. Can you give me a hand?

SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable table = instance.GetDataSources();

foreach (var row in table.Rows)
{
     foreach (var column in table.Columns)
     {
          ddlPublisherServer.Items.Add(???);
     }
}

Where ??? = SQL Server Name.

How would I go about extracting the sql server name from the table?

I'm working in ASP .NET with C#.

like image 918
JJ. Avatar asked Nov 16 '25 14:11

JJ.


1 Answers

Well, if wanted to see all available information, you can iterate over the columns available:

DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow dr in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        Console.WriteLine("{0,-15}: {1}", col.ColumnName, dr[col]);
    }
    Console.WriteLine();
}

Which would show something like:

ServerName     : COMPUTER1
InstanceName   : SQLEXPRESS
IsClustered    : No
Version        : 9.00.4035.00

So, to answer your question:

ddlPublisherServer.Items.Add(row[table.Columns["ServerName"]]);

Full Code:

DataTable table = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow server in table.Rows)
{
  ddlPublisherServer.Items.Add(server[table.Columns["ServerName"]].ToString());
}
like image 95
Brad Christie Avatar answered Nov 19 '25 02:11

Brad Christie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!