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?

5
  • what's the language you're programming in, and entity framework or other, are you using LINQ? Commented Apr 6, 2012 at 12:41
  • I am connecting c# app with access databse,no I am not using LINQ. Commented Apr 6, 2012 at 12:44
  • You still need to show some code, otherwise the best you can hope for here is "well, then do it" Commented Apr 6, 2012 at 12:49
  • What do you want to get? The list of CDGroupID that have 'f1','f2' and 'f3'? Commented Apr 6, 2012 at 12:59
  • @Devart list of CDGroupID with there corrsponding field Commented Apr 6, 2012 at 13:12

4 Answers 4

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()?

Sign up to request clarification or add additional context in comments.

Comments

0
 var listId= string.Join(",", fieldIdList.Select(m => string.Format("'{0}'", m)));;

then

"Field.FieldID in (" + listId+ ")

Comments

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";

Comments

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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.