[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-04-10
5
Medium Priority
?
190 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 225 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 225 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 150 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

649 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