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

Posted on 2009-02-11
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#. ( would be ok too)
Question by:charlesbaldo
    LVL 25

    Accepted Solution

    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

    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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now