Sup, guys. I have a table named 'mains' and 4 columns in it named 'ID', 'Date', 'Adresse' (yea-yea, I know there is mistakes in it) and 'Refills'. I need to INSERT new data if it not exists in table (checking it by ID), or if it exist UPDATE it. Problem is that I getting 'IF syntax error' and I really don't know why. Also, I have some qualm about 'UPDATE' part. Here is my code:
string CommandText = "IF (SELECT '" + Convert.ToInt64(dataGridView2[0, 0].Value) + "' FROM mains IS NULL) INSERT INTO mains (ID,Date,Adresse, Refills) values (" + Convert.ToInt64(dataGridView2[0, 0].Value) + ",'" + Date + "','" + Adresse + "'," + Convert.ToInt64(dataGridView2[3, 0].Value) + ") ELSE UPDATE mains SET Date='" + Date + "', Adresse = '" + Adresse + "', Refills = (Refills + 1) WHERE ID = '" + dataGridView2[0, 0].Value.ToString() + "' END IF";
Without C# (in SQL) it looks like:
IF (SELECT 'numerical id' FROM mains IS NULL) INSERT INTO mains (ID, Date, Adresse, Refills) values (numerical id, 'date converted to string (text)', 'text', number) ELSE UPDATE mains SET Date = 'date converted to string', Adresse = 'text', Refils = (Refils + 1) WHERE ID = 'numerical ID converted to string' END IF