SQL选择在哪里名单LT值;字符串> [英] SQL Select where values in List<string>

查看:191
本文介绍了SQL选择在哪里名单LT值;字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,我可以创建对数据源的查询(可以是SQL,Oracle或访问),有一个where子句指向一个ArrayList或列表?

Is there a way that I can create query against a data source (could be sql, oracle or access) that has a where clause that points to an ArrayList or List?

例如:

Select * from Table where RecordID in (RecordIDList)

我已经看到了一些方法来使用LINQ做,但我宁愿不求助于它,如果它是可以避免的。

I've seen some ways to do it with Linq, but I'd rather not resort to it if it's avoidable.

推荐答案

您可以使用的string.join 。尝试是这样的:

You could use String.Join. Try something like this:

String query = "select * from table where RecordId in ({0});";
String formatted = String.Format(query, String.Join(",", list.ToArray()));

作为一个侧面说明这不会保护您免受SQL注入 - 希望这个例子将指向您在正确的方向。

As a side note this will not protect you against SQL injection - hopefully this example will point you in the right direction.

这篇关于SQL选择在哪里名单LT值;字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆