Solved

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

Posted on 2007-04-10
5
181 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:hbash
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

18 Experts available now in Live!

Get 1:1 Help Now