ASP.net简单删除查询,传入字符串值
DELETE FROM tblArtworkApprovalUsers
WHERE (userID NOT IN (@UserIDList)) AND (approvalID =
(SELECT ID
FROM tblArtworkApprovals
WHERE (templateID = @TemplateID)))
这是在我的表适配器中。 @UserIDList 需要接受类似以下内容:
2,44,12,70
如何使此查询接受该字符串?
DELETE FROM tblArtworkApprovalUsers
WHERE (userID NOT IN (@UserIDList)) AND (approvalID =
(SELECT ID
FROM tblArtworkApprovals
WHERE (templateID = @TemplateID)))
This is in my table adapter. @UserIDList needs to accept something like:
2,44,12,70
How can I make this query accept that string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NOT IN
需要表达式而不是字符串。因此,如果您传递参数而不是动态构造 SQL,则无法完成此操作。另一种方法是动态创建 SQL(同时了解 SQL 注入):
更新
Craig 在此指出了一个更好的解决方案,该解决方案并没有提供更好的性能(因为参数是可变的,而查询计划确实如此)除非完全相同,否则不会被缓存),但有助于 SQL 注入攻击:
参数化 SQL IN 子句
NOT IN <expr>
requires an expression and not a string. So if you are passing the parameter and not constructing the SQL dynamically this cannot be done.Alternative is to create the SQL dynamically (while being aware of SQL Injection):
UPDATE
Craig pointed to a better solution here which does not provide much better performance (since parameters are variable and query plan does not get cached unless it is exactly the same) but help with SQL injection attack:
Parameterize an SQL IN clause
您有几个选择...
You have a couple of options...
您可能想看看 SQL2005 中的数组 和 SQL2008 中的数组,具体取决于 SQL 服务器的版本。
You might want to have a look at Arrays in SQL2005 and Arrays in SQL2008, depending on the version of your SQL server.