How to check for the existence of a DB?

前端 未结 8 2206
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:50

    To cover the range of possibilities (server doesn't exist, database doesn't exist, no login, no permissions, server down, etc) - the simplest idea is simply to try to connect as normal, and perform something trivial - SELECT GETDATE() for example. If you get an exception, there is a problem!

    There are times (especially when dealing with out-of-process systems) when try/catch is the most pragmatic option.

提交回复
热议问题