如何检查DB的存在? [英] How to check for the existence of a DB?

查看:126
本文介绍了如何检查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的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆