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