Solved

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

Posted on 2007-04-10
5
183 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

778 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