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 Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment