J. Ptak said:
Can anyone tell me how to query a SQL Server for a list of
the databases hosted by that server?
Also, is there a method in the .Net Framework to query the
network for a list of SQL Servers that are available?
It was a while ago that I did this, but SqlBuddy lists databases using the
following query:
select dbs.name,
dbs.dbid,
dbs.version,
dbs.filename,
dbs.cmptlevel,
cast(dbs.crdate as varchar(50)) as crdate,
HAS_DBACCESS( dbs.name ) as HasAccess
from master.dbo.sysdatabases dbs
order by dbs.name
Note that originally I wanted to use the standards compliant
INFORMATION_SCHEMA views, but I think this produced problems where some of
my user accounts on certain servers didn't have select privalidges against
these views, so therefore the queries produced errors. The query I *wanted*
to use was this:
select dbs.name,
dbs.dbid,
dbs.version,
dbs.filename,
dbs.cmptlevel,
schem.schema_owner as owner,
cast(dbs.crdate as varchar(50)) as crdate,
HAS_DBACCESS( dbs.name ) as HasAccess
from master.dbo.sysdatabases dbs
inner join INFORMATION_SCHEMA.SCHEMATA schem on
schem.catalog_name = dbs.name
order by dbs.name
As someone else pointed out, you may be better off avoiding sysdatabases
because of future compatibility issues.
Hope this helps,
Tobes
p.s - SqlBuddy is open source, source available through
http://sqlbuddy.sourceforge.net