I've got an Asp.Net application with a MySql database. To access this database I use a dataset (.xsd) where I've put my queries in TableAdapters. One of these queries has a WHERE IN
clause, like:
SELECT somefield
FROM sometable
WHERE somefield IN (?)
This somefield
is a string value. The TableAdapter function expects a string parameter to replace the query parameter. However, I would like to pass a List<string>
(or an array, or any other type) to check for multiple values. The only way I've found so far is to convert the list into a comma separeted list:
GetData(string.Join(",", myList.ToArray());
It works fine as long as there's only one value in the list. With two values the query returns 0 rows. I activated the general log in MySql and found out it has to do with quotes. The query that reaches the database is:
SELECT somefield
FROM sometable
WHERE somefield IN ('foo,bar')
I added extra quotes when joining the values:
GetData(string.Join("','", myList.ToArray());
But the extra quotes only get escaped and the result still is 0 rows:
SELECT somefield
FROM sometable
WHERE somefield IN ('foo\',\'bar')
Right now the only option I see is converting every single string into an id and join these integers into a string. Like in this thread. But this means a lot of extra calls to the database to get those id's. I really prefer to use the string values!
Who knows how to solve this puzzle?