Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for the existence of a DB?

I am wondering if there is an elegant way to check for the existence of a DB? In brief, how do test the connection of a db connection string?

Thanks

like image 202
Martin Avatar asked May 16 '09 20:05

Martin


People also ask

How can I tell if a SQL Server database is being used?

Another way to see if your database is in use is to look and see if the indexes are being used. Information on index usage is held in the sys. dm_db_index_usage_stats table since the last server reboot, and can be queried using this statement which can be tailored to select the data you need.


1 Answers

Set the Initial Catalog=master in the connection string and execute:

select count(*) from sysdatabases where name = @name

with @name set to the name of the database.

If you want to check the connection string as a whole (and not existence of an independent database), try connecting to it in a try/catch block.

like image 92
mmx Avatar answered Nov 02 '22 12:11

mmx