今天用Dapper更新是用到了IN写法,园子里找了篇文章这样写到
传统sql in (1,2,3) 用dapper就这样写
conn.Query<Users>("SELECT * FROM Users s WHERE s.id IN (@ids) ",new { ids = new int[]{1,2,3}}) conn.Query<Users>("SELECT * FROM Users s WHERE s.id IN (@ids) ",new { ids = IDs.ToArray()})
用了之后出现,
System.Data.SqlClient.SqlException:““,”附近有语法错误。”
这样的提示,
跟踪SQL语句时发现按以上方法生成的SQL语句是这样的:
exec sp_executesql N'update WebSiteChanelListPage set status=0 where ID IN ((@ilist1,@ilist2))',N'@ilist1 int,@ilist2 int',@ilist1=1,@ilist2=2
我们不难发现生成的语句中多了一层括号,于是果段修改代码:
string UpdateString = "update WebSiteChanelListPage set status=0 where ID IN @ilist";
connection.Execute(UpdateString,new { ilist = idlist.ToArray() });
执行成功!问题解决!
当然也有可能是我的Dapper的版本问题,我用得是:Dapper 1.50.4.0