Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I check if a table exists?

How can I check if a table exists?

Scheduled Pinned Locked Moved C#
databasehelpquestioncsharptutorial
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Emmet_Brown
    wrote on last edited by
    #1

    can anyone help? I tried to google out the solution but none of the answers I found was non proper for C#. I've a MS SQL Database (Compact Edition(this is not an issue here)) I want to check if a table exists, what should I do? I found an SQL statement for this IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U')) but I could not figure out how to use this in C# My first idea is=

    string command2 = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))"
    SqlCeCommand com2 = new SqlCeCommand(command2, conn);

    Where should I look if it returned 1 or 0?

    C T P 3 Replies Last reply
    0
    • E Emmet_Brown

      can anyone help? I tried to google out the solution but none of the answers I found was non proper for C#. I've a MS SQL Database (Compact Edition(this is not an issue here)) I want to check if a table exists, what should I do? I found an SQL statement for this IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U')) but I could not figure out how to use this in C# My first idea is=

      string command2 = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))"
      SqlCeCommand com2 = new SqlCeCommand(command2, conn);

      Where should I look if it returned 1 or 0?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      SQLCe will not support [dbo]. at the head of a table name. I don't know if it supports this sort of call ( I've never tried it ). Remove the [dbo]. and then complete the statement ( such as if EXISTS... select 1 ELSE select 0 ) then you can execute scalar and see if you get 1 or 0 back. You can experiment inside SQL Server management console to establish exactly what SQL works. Your alternative is to say select * from tablename and do it in a try catch to see if it blows up ( presumably b/c the table does not exist )

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      E 1 Reply Last reply
      0
      • C Christian Graus

        SQLCe will not support [dbo]. at the head of a table name. I don't know if it supports this sort of call ( I've never tried it ). Remove the [dbo]. and then complete the statement ( such as if EXISTS... select 1 ELSE select 0 ) then you can execute scalar and see if you get 1 or 0 back. You can experiment inside SQL Server management console to establish exactly what SQL works. Your alternative is to say select * from tablename and do it in a try catch to see if it blows up ( presumably b/c the table does not exist )

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        E Offline
        E Offline
        Emmet_Brown
        wrote on last edited by
        #3

        thanks I'll try furthermore, just to know Can I use this "if -> else" format like below? IF (blahblahblah) >0 BEGIN CREATE TABLE MyTable (...etc) END"

        C 1 Reply Last reply
        0
        • E Emmet_Brown

          can anyone help? I tried to google out the solution but none of the answers I found was non proper for C#. I've a MS SQL Database (Compact Edition(this is not an issue here)) I want to check if a table exists, what should I do? I found an SQL statement for this IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U')) but I could not figure out how to use this in C# My first idea is=

          string command2 = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))"
          SqlCeCommand com2 = new SqlCeCommand(command2, conn);

          Where should I look if it returned 1 or 0?

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #4

          Just execute the command, but a better command would be

          string command2 = "Select Case OBJECT_ID('TableName') when null then 0 else 1 end;";
          SqlCeCommand com2 = new SqlCeCommand(command2, conn);
          bool tableExists = (bool)com2.ExecuteScalar();

          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

          E 2 Replies Last reply
          0
          • T The Man from U N C L E

            Just execute the command, but a better command would be

            string command2 = "Select Case OBJECT_ID('TableName') when null then 0 else 1 end;";
            SqlCeCommand com2 = new SqlCeCommand(command2, conn);
            bool tableExists = (bool)com2.ExecuteScalar();

            If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

            E Offline
            E Offline
            Emmet_Brown
            wrote on last edited by
            #5

            oh this works? I'll try right now :D

            1 Reply Last reply
            0
            • E Emmet_Brown

              thanks I'll try furthermore, just to know Can I use this "if -> else" format like below? IF (blahblahblah) >0 BEGIN CREATE TABLE MyTable (...etc) END"

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I think so. I've never executed SQL that complex outside of a proc, but I don't see why you shouldn't be able to.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              E 1 Reply Last reply
              0
              • E Emmet_Brown

                can anyone help? I tried to google out the solution but none of the answers I found was non proper for C#. I've a MS SQL Database (Compact Edition(this is not an issue here)) I want to check if a table exists, what should I do? I found an SQL statement for this IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U')) but I could not figure out how to use this in C# My first idea is=

                string command2 = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TableName]') AND type in (N'U'))"
                SqlCeCommand com2 = new SqlCeCommand(command2, conn);

                Where should I look if it returned 1 or 0?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I would likely use SELECT COUNT(*) FROM sys.objects ... to see how many tables have that name and type.

                1 Reply Last reply
                0
                • T The Man from U N C L E

                  Just execute the command, but a better command would be

                  string command2 = "Select Case OBJECT_ID('TableName') when null then 0 else 1 end;";
                  SqlCeCommand com2 = new SqlCeCommand(command2, conn);
                  bool tableExists = (bool)com2.ExecuteScalar();

                  If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                  E Offline
                  E Offline
                  Emmet_Brown
                  wrote on last edited by
                  #8

                  The function is not recognized by SQL Server Compact Edition. [ Name of function = OBJECT_ID,Data type (if known) = ] ..it says pointing out the bool tableExists = (bool)com2.ExecuteScalar(); line :/

                  T 1 Reply Last reply
                  0
                  • C Christian Graus

                    I think so. I've never executed SQL that complex outside of a proc, but I don't see why you shouldn't be able to.

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                    E Offline
                    E Offline
                    Emmet_Brown
                    wrote on last edited by
                    #9

                    according to compiler Compact Edition does not support the function OBJECT_ID() I guess try-catch is the only solution is it?

                    C 1 Reply Last reply
                    0
                    • E Emmet_Brown

                      according to compiler Compact Edition does not support the function OBJECT_ID() I guess try-catch is the only solution is it?

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      I would presume so. I am not surprised, CE is a very cut down version, designed to be portable, not complete.

                      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                      1 Reply Last reply
                      0
                      • E Emmet_Brown

                        The function is not recognized by SQL Server Compact Edition. [ Name of function = OBJECT_ID,Data type (if known) = ] ..it says pointing out the bool tableExists = (bool)com2.ExecuteScalar(); line :/

                        T Offline
                        T Offline
                        The Man from U N C L E
                        wrote on last edited by
                        #11

                        SQL CE. silly me.

                        string command2 = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'your table name'";
                        SqlCeCommand com2 = new SqlCeCommand(command2, conn);
                        bool tableExists = false;
                        If ((Integer)com2.ExecuteNonQuery() > 0){
                        tableExists = true;
                        }

                        ExecuteNonQuery returns the number of rows selected and if the table exists the return value will be 1.

                        If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                        E 1 Reply Last reply
                        0
                        • T The Man from U N C L E

                          SQL CE. silly me.

                          string command2 = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'your table name'";
                          SqlCeCommand com2 = new SqlCeCommand(command2, conn);
                          bool tableExists = false;
                          If ((Integer)com2.ExecuteNonQuery() > 0){
                          tableExists = true;
                          }

                          ExecuteNonQuery returns the number of rows selected and if the table exists the return value will be 1.

                          If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) www.JacksonSoft.co.uk

                          E Offline
                          E Offline
                          Emmet_Brown
                          wrote on last edited by
                          #12

                          oh thanks =)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups