0

I am attempting to query an MS Access DB with C#

Static query is -

Select FieldID , CDGroups.CDGroupID , Priority
from Fields , CDGroups
where Fields.CDGroupID = CDGroups.CDGroupID
  and Fields.FieldID in ('f1','f2','f3')
order by Priority

I need to replace f1,f2.. FieldID from fieldIdList(List<string> fieldIdList) which contains all these fields

How can I go about it?

Andriy M
  • 76,112
  • 17
  • 94
  • 154
NIlesh Lanke
  • 1,213
  • 9
  • 26
  • 35

4 Answers4

1

The correct way would be to use SQL parameters.
To do this, you need to loop through your list and create one parameter per list item - in the SQL string and in your query's list of SQL parameters.

Take a look at this answer to a similar question:
How to pass sqlparameter to IN()?

Community
  • 1
  • 1
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
0
 var listId= string.Join(",", fieldIdList.Select(m => string.Format("'{0}'", m)));;

then

"Field.FieldID in (" + listId+ ")
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

I used following code:

 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";
NIlesh Lanke
  • 1,213
  • 9
  • 26
  • 35
0

Try this query -

SELECT CDGroupID FROM fields
  WHERE FieldID IN ('f1', 'f2', 'f3')
  GROUP BY CDGroupID
  HAVING(COUNT(DISTINCT FieldID)) >= 3

It will show a list of CDGroupID that have 'f1','f2' and 'f3' at least. If you want to show ones that have just 'f1','f2' and 'f3', then change WHERE condition to 'HAVING(COUNT(DISTINCT FieldID)) >= 3'.

Devart
  • 119,203
  • 23
  • 166
  • 186