?
Solved

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

Posted on 2009-02-11
4
Medium Priority
?
1,908 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)
0
Comment
Question by:Charles Baldo
4 Comments
 
LVL 25

Accepted Solution

by:
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>
		[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

0
 
LVL 11

Expert Comment

by:indianguru2
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.
0
 
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.

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

Author Closing Comment

by:Charles Baldo
ID: 31545678
Thanks
0

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