C# SQL create table IF it doesn't already exist
- by jakesankey
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();