Enumerating the databases on a server in SQLDMO/vb.net

|

Whilst you could just execute the following TSQL command:

select name from master..sysdatabases

to obtain the list of databases on a SQL Server box, it's far more fun to try and do it the SQLDMO way ;) Which is....

Dim oSqlServer as SQLDMO.SQLServer2 = New SQLDMO.SQLServer2Class
Dim oSqlServerDB as SQLDMO.Database

oSqlServer.Connect("ServerName", "SQL_Login_Name, "SQL_Password")

For Each oSqlServerDB In oSqlServer.Databases
MessageBox.Show(oSqlServerDB.Name)
Next

I've used MessageBox.Show to output the database names, but I could've just as easily have pushed the details of each database into an ArrayList or other collection and returned the information as the output of a function. Another way of writing the above code (not as easy to read it has to be said! Well, not as "OOP" anyway!) is:

For i As Integer = 1 To m_oSqlServer.Databases.Count
MessageBox.Show(m_oSqlServer.Databases.ItemByID(i).Name)
Next

And that, pretty much, is how to enumerate the databases on a SQL Server via SQLDMO

About this Entry

This page contains a single entry by Robert Wray published on May 14, 2005 11:57 PM.

Using SQLDMO in .net (VB.net) was the previous entry in this blog.

HttpModule in .net is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 5.04