添加列表<&诠释GT;到MySQL参数 [英] Add List<int> to a mysql parameter

查看:152
本文介绍了添加列表<&诠释GT;到MySQL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从.NET连接器了MySQLParameter这个问题。



我有这个疑问:

  SELECT * FROM表WHERE ID IN(@parameter)

而且是了MySQLParameter:

  intArray =新的List< INT>(){1,2,3,4}; 

... connection.Command.Parameters.AddWithValue(参数,intArray);

这是可能的吗?
是可能的一个int数组传递给一个了MySQLParameter?
其他的解决方案将是int数组转换为一个字符串,如1,2,3,4,但这个,当我把它传递给了MySQLParameter,这被认为是一个字符串,它把在。SQL查询,如1\,2\,3\,4,这不返回预期值



@更新:好像在mysql连接器的团队应该工作有点困难。


解决方案

当我把它传递给了MySQLParameter这被公认为是一个字符串,它把在SQL查询,如1\,2\,3\,4,这不返回预期值。




我遇到了这个昨晚。我发现FIND_IN_SET在这里工作:

  SELECT * FROM表WHERE FIND_IN_SET(ID,@parameter)= 0 
! ...
intArray =新的List< INT>(){1,2,3,4};
conn.Command.Parameters.AddWithValue(参数,的string.join(,,intArray));



显然,这有一定长度的限制(我发现您的文章寻找一个替代的解决方案),但这可能为你工作。


I have this question about the MySqlParameter from the .NET connector.

I have this query:

SELECT * FROM table WHERE id IN (@parameter)

And the MySqlParameter is:

intArray = new List<int>(){1,2,3,4};

...connection.Command.Parameters.AddWithValue("parameter", intArray);

This is possible? Is possible to pass an array of int to a single MySqlParameter? The other solution will be convert the array of int to a string such like "1,2,3,4", but this, 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.

@ UPDATE: Seems like the mysql connector team should work a little bit harder.

解决方案

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.

I ran into this last night. I found that FIND_IN_SET works here:

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

Apparently this has some length limitations (I found your post looking for an alternate solution), but this may work for you.

这篇关于添加列表&LT;&诠释GT;到MySQL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆