C# SQL create table IF it doesn't already exist

Posted by jakesankey on Stack Overflow See other posts from Stack Overflow or by jakesankey
Published on 2010-05-27T17:39:23Z Indexed on 2010/05/27 17:41 UTC
Read the original article Hit count: 182

Filed under:
|

Hey, I am trying to put a little logic into my C# app that will create a table called Import, IF it doesn't already exist.. here is my code, it doesn't seem to work tho.

con.Open();
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText =
                            @" 
                       IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'RX_CMMData' AND TABLE_NAME = 'Import'))
BEGIN
CREATE TABLE Import (
  RowId     integer PRIMARY KEY NOT NULL,
  PartNumber  varchar(200) NOT NULL,
  CMMNumber   varchar(200) NOT NULL,
  Date        varchar(200) NOT NULL,
  FeatType    varchar(200) NOT NULL,
  FeatName    varchar(200) NOT NULL,
  Value       varchar(200) NOT NULL,
  Actual      decimal,
  Nominal     decimal,
  Dev         decimal,
  TolMin      decimal,
  TolPlus     decimal,
  OutOfTol    decimal,
  FileName    varchar(200) NOT NULL
); END";
                        cmd.ExecuteNonQuery();
                    }
                    con.Close();

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql