Thomas D Hall
Thomas D Hall

Reputation: 21

Using SMO for retrieving SQL Server, Database, Table info

I get the db names using:

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

Then using the below I get a list of db's.

static void Main(string[] args)
        {
            {
                //Connect to the local, default instance of SQL Server. 
                Server srv;
                srv = new Server("LAPTOP\\SQLEXPRESS");
                //The connection is established when a property is requested.

                Console.WriteLine(srv.Databases.ToString());
                foreach (Database db in srv.Databases)
                {
                    Console.WriteLine(db);
                }
                Console.ReadLine();
            }
        }

The problem is that I only get the system databases, no others, even though they exist. I don't know what to change to get the info for all of the databases. Any assistance would be appreciated.

Upvotes: 2

Views: 5297

Answers (1)

Furqan Safdar
Furqan Safdar

Reputation: 16718

Though its late but i just got your question unanswered as per your requirement. Why don't you try this piece of code snippet:

Server srvMgmtServer = default(Server);
srvMgmtServer = new Server("LAPTOP\\SQLEXPRESS");
ServerConnection srvConn = default(ServerConnection);
srvConn = srvMgmtServer.ConnectionContext;
srvConn.LoginSecure = true;

foreach(Database db in srvMgmtServer.Databases;
{
    Console.WriteLine(db.Name);
}

Upvotes: 4

Related Questions