0

I want to list all SQL server instances and their tables.

I have code that lists all the servers correctly but I cannot seem to get a list of their tables.

        DataTable dataSources = SqlDataSourceEnumerator.Instance.GetDataSources();
        foreach (DataRow row in dataSources.Rows)
        {
            Console.WriteLine("Server Name:" + row["ServerName"]);

            foreach (var item in row.ItemArray)
            {
                Console.WriteLine(" - Item: "+ item);
            }
        }
Chris
  • 26,744
  • 48
  • 193
  • 345
  • possible duplicate of [How to list available instances of SQL Servers using SMO in C#?](http://stackoverflow.com/questions/1130580/how-to-list-available-instances-of-sql-servers-using-smo-in-c) – Mitch Wheat Aug 04 '10 at 14:03
  • That shows how to get server names not table names, thanks though – Chris Aug 04 '10 at 14:06

1 Answers1

2

You can query what tables are in a db using sys.Tables. See below:

USE YourDBName
GO
SELECT name FROM sys.Tables
GO 
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486