How to check for the existence of a DB?

前端 未结 8 2218
Happy的楠姐
Happy的楠姐 2020-12-18 05:52

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

8条回答
  •  悲&欢浪女
    2020-12-18 06:24

    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.

提交回复
热议问题