If I run the following in SSMS, the table gets created if it does not exist. However, in my SQLCommand code, it connects and sends the query through with no error but does not create the table if it doesn't exist. Any ideas?
string[] tables = new string[6];
tables[0] += ("IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[AD_Authorization]') AND type in (N'U')) " +
"CREATE TABLE [AD_Authorization]([ID] [bigint] IDENTITY(1,1) NOT NULL, " +
" [AD_Account] [varchar](255) NOT NULL, " +
" [AD_SID] [varchar](255) NOT NULL, " +
" [AD_EmailAddress] [varchar](255) NULL, " +
" [DateImported] [datetime] NOT NULL, " +
" [Active] [bit] NULL) ON [PRIMARY]");
for (int i = 0; i <= 5; i++)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(tables[i], connection))
{
connection.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
}
}
}
sys.tablesrather thansys.objects(and having to specify what type of object you're interested in) -IF NOT EXISTS (SELECT * FROM sys.tables WHERE Name = 'AD_Authorization') ....- but I doubt this'll make any difference