我正在尝试使用MS Access
C#
数据库
静态查询是 -
Select FieldID , CDGroups.CDGroupID , Priority
from Fields , CDGroups
where Fields.CDGroupID = CDGroups.CDGroupID
and Fields.FieldID in ('f1','f2','f3')
order by Priority
我需要替换包含所有这些字段的fieldIdList(List<string> fieldIdList)
的f1,f2 .. FieldID
我该怎么办呢?
答案 0 :(得分:1)
正确的方法是使用SQL参数 为此,您需要遍历列表并为每个列表项创建一个参数 - 在查询的SQL参数列表中的SQL字符串和中。
看一下类似问题的答案:
How to pass sqlparameter to IN()?
答案 1 :(得分:0)
var listId= string.Join(",", fieldIdList.Select(m => string.Format("'{0}'", m)));;
然后
"Field.FieldID in (" + listId+ ")
答案 2 :(得分:0)
我使用了以下代码:
StringBuilder sb = new StringBuilder();
foreach (string fieldId in fieldIdList)
{
sb.Append("'" + fieldId + "',");
}
string fieldList = sb.ToString().TrimEnd(',');
string queryString =
"Select FieldID , CDGroups.CDGroupID , Priority from Fields , CDGroups where Fields.CDGroupID = CDGroups.CDGroupID and Fields.FieldID in (" + fieldList + ") order by Priority";
答案 3 :(得分:0)
尝试此查询 -
SELECT CDGroupID FROM fields
WHERE FieldID IN ('f1', 'f2', 'f3')
GROUP BY CDGroupID
HAVING(COUNT(DISTINCT FieldID)) >= 3
它将显示至少包含'f1','f2'和'f3'的CDGroupID列表。如果你想显示只有'f1','f2'和'f3'的那些,那么将WHERE条件改为'HAVING(COUNT(DISTINCT FieldID))&gt; = 3'。