我有这个关于 .NET 连接器的 MySqlParameter 的问题。
我有这个问题:
SELECT * FROM table WHERE id IN (@parameter)
而MySqlParameter是:
intArray = new List<int>(){1,2,3,4};
...connection.Command.Parameters.AddWithValue("parameter", intArray);
这可能吗? 是否可以将 int 数组传递给单个 MySqlParameter? 另一种解决方案是将int数组转换为诸如“1,2,3,4”之类的字符串,但是,当我将它传递给MySqlParameter并且这被识别为字符串时,它会像这样放入sql查询"1\,2\,3\,4"这不会返回预期值。
@ UPDATE: mysql 连接器团队似乎应该更加努力。
最佳答案
when i pass it to the MySqlParameter and this is recognized as a string, it puts in the sql query like "1\,2\,3\,4" and this do not return the expected values.
我昨晚遇到了这个。我发现 FIND_IN_SET 在这里工作:
SELECT * FROM table WHERE FIND_IN_SET(id, @parameter) != 0
...
intArray = new List<int>(){1,2,3,4};
conn.Command.Parameters.AddWithValue("parameter", string.Join(",", intArray));
显然这有一些长度限制(我发现您的帖子正在寻找替代解决方案),但这可能对您有用。
关于c# - 将 List<int> 添加到 mysql 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5681320/