Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to check if a table exist in a database in sql server 2008 using c#.
Posted

like that:

C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_DbTableExistance
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = @"data source=.\sqlexpress; initial catalog=KKD; integrated security=true";
            string tableQuery = @"select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME='{0}'";

            try
            {
                string cmdText = string.Format(tableQuery, "DISPLAY_STAT");
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                    {
                        object o = cmd.ExecuteScalar();
                        Console.WriteLine(o == null ? "none" : "exists");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900