Why am I unable to create a trigger using my SqlCommand?

可紊 提交于 2019-12-24 02:45:10

问题


The 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; 

The exception:

Incorrect syntax near the keyword 'TRIGGER'.

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


回答1:


Do you have CommandType set wrong?




回答2:


Options

  • The 1st line of the actual SQL sent is not CREATE TRIGGER
  • CommandType is wrong (eg it's trying to add EXEC or some "prepare" commands)

Use SQL profiler to see exactly what you're sending on to the DB engine (you actually have Express edition that is hosting the MDF)




回答3:


I am not sure why this is failing but if I were you I would use SMO for ddl queries. In this case you need Trigger.Create Method.



来源:https://stackoverflow.com/questions/2868704/why-am-i-unable-to-create-a-trigger-using-my-sqlcommand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!