We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
2,243 Views
Last Modified: 2012-05-06
Hello

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)
Comment
Watch Question

Commented:
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>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			try
			{
				SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=<server>,1433;database=tempdb;User id=<user id>;Password=<password>;");
				thisConnection.Open();
				SqlCommand thisCommand = thisConnection.CreateCommand();
				thisCommand.CommandText = "SELECT DBName = name FROM master..sysdatabases WHERE name = '<dbname>';
				SqlDataReader thisReader = thisCommand.ExecuteReader();
				thisReader.Read())
				if thisReader["DBName"] > 0
				     Console.WriteLine("Database exists");
				else				
				     Console.WriteLine("Database does not exist");
				thisReader.Close();
				thisConnection.Close();
 
			}
			catch (SqlException e)
			{
				Console.WriteLine(e.Message);
			}
			
		}
	}
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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.
Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT
Commented:
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.

http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/7f21cb27-c939-4a89-98d7-0a8ec7ae0d8b/
Charles BaldoSoftware Developer

Author

Commented:
Thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.