Other Posts in Database

  1. SQL Helper Class and Jack Thompson
  2. Determining if a Database or Table Exists in SQL Server
  3. Helpful SQL Queries for SQL Server in C#
  4. Simplified Database Creation and Pulling Database Structure in C#

Determining if a Database or Table Exists in SQL Server


This is, most likely, not going to be that practical for really anyone out there. But since I'm working on the ORM, I came across a way of determining whether or not a database exists in SQL Server. So let's look at the code:

   1: SELECT * FROM Master.sys.Databases where name='DatabaseName'

Obviously this can be modified but what you should learn from the above one liner is that database information can be found in the Master.sys.Databases table. And in fact we have a very similar one liner that can be used to determine whether or not a table exists within a database:

   1: SELECT * FROM sys.Tables where name='TableName'

Note that the above is using the database that you want to search within and not the master database. Anyway, that's all there is to it. But then again where you'd end up using this, I don't know. Anyway, hope this helps out someone. So try it out and happy coding.