Solved

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

Posted on 2007-04-10
5
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

690 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