Solved

Iterate through an MS SQL Server for all it's Databases

Posted on 2007-04-10
5
182 Views
Last Modified: 2008-03-06
I am writing an application that need to iterate through an MS SQL Server and list off all the databases contained within a given server.  I have been hunting for some ADO or ADOX object that offers this functionality.  But,  so far,  I have not what I need.  It would seem that such an object would be similar to the ADOX catalog object that allows me to iterate through a connection's tables/views/stored procedures.

Any suggestions would be greatly appreciated.

Thanks,
Howard
0
Comment
Question by:Howard Bash
5 Comments
 
LVL 11

Accepted Solution

by:
Otana earned 75 total points
ID: 18882295
Run this on your SQL Server, it will return you a list of all the databases on it:

select * from master.dbo.sysdatabases
0
 
LVL 7

Assisted Solution

by:badbearontour
badbearontour earned 75 total points
ID: 18882318


use master

select * from sysdatabases


BB
0
 
LVL 7

Expert Comment

by:badbearontour
ID: 18882323
Sorry Otana, I was typing my answer as you were typing too...

BB
0
 
LVL 6

Assisted Solution

by:chiragkhabaria
chiragkhabaria earned 50 total points
ID: 18882447
Well, there is a undocumented stored procedure which can you used for itrating through all the database

EXEC sp_MSforeachdb @command1='PRINT ''Listing ?''', @command2='USE ? SELECT DB_NAME()'
0
 
LVL 2

Assisted Solution

by:soco180
soco180 earned 50 total points
ID: 18882737
Create an ADO Connection to the SQL Server.
Create an ADO Recordset and execute the T-SQL expression listed by Otana.

In your application it goes something like this.

Public Sub LoopSQLServerDB()
    Dim MyCon As ADO.Connection
    Dim MyRs As ADO.Recordset
    Dim sDBName As String
    Dim sDataSource As String
   
    sDataSource = "TheNameOfYourSQLServer"
   
    Set MyCon = New ADO.Connection
   
    MyCon.ConnectionString = "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
                "trusted_connection=yes;" & _
                "Persist Security Info=True;" & _
                "Initial Catalog=Master" & _
                ";Data Source=" & sDataSource
   
    MyCon.Open
    Set MyRs = MyCon.Execute("Select * from sysdatabases")
   
    Do While Not MyRs.EOF
        sDBName = MyRs.Fields("name")
        MyRs.MoveNext
    Loop
End Sub
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now