C#, sp_executesql and Incorrect Syntax

后端 未结 1 935
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 00:13

I\'m calling the code below.

On the line (IDataReader dr = cmd.ExecuteReader()) sql barfs with an Incorrect syntax near \'CompanyUpdate\'.

1条回答
  •  被撕碎了的回忆
    2021-01-19 00:50

    I notice that you've not set the CommandType to StoredProcedure... I don't know if that's the cause of your problem or not:

    cmd.CommandType = CommandType.StoredProcedure;
    

    I've done this so many times myself I can't count.

    Tip to trigger your memory when this throws exceptions next time:

    Have SQL Query Profiler open while you're running your app. When each command executes, it shows the SQL generated and run on the server side. If the SQL generated begins with sp_executesql followed by your query then it's being run as a regular query - i.e. cmd.CommandType = CommandType.Text, if it starts with exec, chances are it's run as a stored proc. Make sure you're getting the correct SQL generated for the type of query you're trying to run.

    0 讨论(0)
提交回复
热议问题