SQL选择在哪里名单LT值;字符串>字符串、名单、SQL、GT

2023-09-03 12:25:56 作者:甜过不二家

有没有一种方法,我可以创建对数据源的查询(可以是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.

 
精彩推荐
图片推荐