How do I check to see if a sql database exists in c#

Posted on 2009-02-11
Medium Priority
Last Modified: 2012-05-06

Can someone please give me a code snippet of or a link with code to tell how to detect if a SQL server database exists using c#. (vb.net would be ok too)
Question by:Charles Baldo
LVL 25

Accepted Solution

reb73 earned 1600 total points
ID: 23614223
Something like this -

using System;
using System.Data.SqlClient;
namespace ConsoleCSharp
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class DataReader_SQL
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
			// TODO: Add code to start application here
				SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=<server>,1433;database=tempdb;User id=<user id>;Password=<password>;");
				SqlCommand thisCommand = thisConnection.CreateCommand();
				thisCommand.CommandText = "SELECT DBName = name FROM master..sysdatabases WHERE name = '<dbname>';
				SqlDataReader thisReader = thisCommand.ExecuteReader();
				if thisReader["DBName"] > 0
				     Console.WriteLine("Database exists");
				     Console.WriteLine("Database does not exist");
			catch (SqlException e)

Open in new window

LVL 11

Expert Comment

ID: 23614225
In checking if your database exists... You have to catch it in your exception. Try to play around connecting with an existing database and a non-existing one. Then you'll get the exception it will throw if the database is non-existing.
LVL 17

Assisted Solution

by:Daniel Reynolds
Daniel Reynolds earned 400 total points
ID: 23614251
This link shows the code you use in T-SQL to check if a database exists.

You could encapsulate that in a regular stored procedure and return a value that would let you know if it exists or doesn't exist.


Author Closing Comment

by:Charles Baldo
ID: 31545678

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question