如何检查DB的存在?

编程入门 行业动态 更新时间:2024-10-26 04:29:42
本文介绍了如何检查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的存在?

本文发布于:2023-11-10 03:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!