Why can't I create a trigger using my SqlCommand?

Line cmd.ExecuteNonQuery();

cmd.CommandText

CREATE TRIGGER  subscription_trig_0  ON  subscription   AFTER INSERT  AS  UPDATE user_data SET msg_count=msg_count+1 FROM user_data
JOIN INSERTED ON user_data.id = INSERTED.recipient; 

      

An exception:

Incorrect syntax near the keyword 'TRIGGER'.

      

Then, using VS 2010 connected to the same file (mdf file), I run the request above and I get a success message. WTF!

+2


a source to share


3 answers


Do you have the CommandType

wrong one?



+1


a source


Functions

  • The first line of the actual sent SQL is not CREATE TRIGGER
  • CommandType is incorrect (for example, it tries to add EXEC or some "prepare" commands)


Use a SQL profiler to see exactly what you are sending to the DB engine (you actually have the Express Edition hosting the MDF)

+2


a source


I'm not sure why this is failing, but if I were you, I would use SMO for ddl requests. In this case, you need Trigger.Create Method.

0


a source







All Articles