user259286
user259286

Reputation: 1005

How would one List the Databases in a SQL Server, not by query, but by c# call?

From this: Enumerate all running databases

One can list the servers on the network, but once one selects one of those servers, how does one then list the DB's in that server, by using a similar method as above?

Thanks!

Upvotes: 3

Views: 396

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239684

You can use SQL Management Objects (SMO). First, you'd use the SmoApplication class and one of its EnumAvailableSqlServers methods to find the server.

Once you've found the server you want, you'd create a Server instance, and then use its Databases property.

Upvotes: 1

Ron Harlev
Ron Harlev

Reputation: 16673

Most interactions with a database server eventually translate to SQL. Even if they look like some other API in a higher level. Look for the SQL to do this and just call it from C#

UPDATE: From here

----SQL SERVER 2005 System Procedures
EXEC sp_databases
EXEC sp_helpdb
----SQL 2000 Method still works in SQL Server 2005
SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases
----SQL SERVER Un-Documented Procedure
EXEC sp_msForEachDB 'PRINT ''?'''

Upvotes: 1

Related Questions