本文介绍了如何检查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?
感谢
推荐答案在连接字符串中设置初始目录=主并执行:
Set the Initial Catalog=master in the connection string and execute:
select count(*) from sysdatabases where name = @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.
更多推荐
如何检查DB的存在?
发布评论