Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the simplest way to check if database exists in MSSQL using VB.NET?

I'm trying to check if a database exists in the Microsoft SQL Server, what's the simplest way to do that? I just want it to return a true or false value then I would create a database if it doesn't exist. Any help would be appreciated, thanks.

like image 275
Nyawk Nyawk Avatar asked Jan 08 '23 07:01

Nyawk Nyawk


1 Answers

Connect to a system-db (master, msdb, tempdb or model) - because you can be sure that they exist! Then you can select the list of database like this:

select * from sys.databases 

or if you want to know if a specific db exists:

select * from sys.databases where name = 'NameOfYourDb'

If you connect without a database name in your connection string (belongs to which provider you are using) you should automatically be connect to your default database (which is "master" by default)

like image 79
CeOnSql Avatar answered Jan 10 '23 19:01

CeOnSql