Mysql multiple addwithvalue:一个返回值就足够了

时间:2013-03-10 15:45:19

标签: c# mysql parameters

我的查询有多个addwithvalue值

        IDCheck.CommandText = "SELECT * FROM cloudposgebruikers WHERE id = @id AND licentie1hc = @lc1 AND licentie2hc = @lc2"
        + " AND licentie3hc = @lc3 AND licentie4hc = @lc4 AND licentie5hc = @lc5 AND licentie6hc = @lc6 AND licentie7hc = @LC7 AND licentie8hc = @lc8"
        + " AND licentie9hc = @lc9 AND licentie10hc = @lc10";
        IDCheck.Parameters.AddWithValue("@id", licenseid);
        IDCheck.Parameters.AddWithValue("lc1", GetId);
        IDCheck.Parameters.AddWithValue("lc2", GetId);
        IDCheck.Parameters.AddWithValue("lc3", GetId);
        IDCheck.Parameters.AddWithValue("lc4", GetId);
        IDCheck.Parameters.AddWithValue("lc5", GetId);
        IDCheck.Parameters.AddWithValue("lc6", GetId);
        IDCheck.Parameters.AddWithValue("lc7", GetId);
        IDCheck.Parameters.AddWithValue("lc8", GetId);
        IDCheck.Parameters.AddWithValue("lc9", GetId);
        IDCheck.Parameters.AddWithValue("lc10", GetId);

当然,这将比较所有这些值,只有在所有值都存在时才返回

假设只有“licentie5hc”和“id”匹配=>返回true

或者说只有“id”和licentie1hc“匹配.. =>返回true

这可能吗?我知道我可以使用不同的查询,但我只需要“id”和其中一个“licentie x hc”参数来匹配......

2 个答案:

答案 0 :(得分:2)

试试这个:

IDCheck.CommandText = "SELECT * FROM cloudposgebruikers 
    WHERE id = @id AND (licentie1hc = @lc1 OR licentie2hc = @lc2 
        OR licentie3hc = @lc3 OR licentie4hc = @lc4 OR licentie5hc = @lc5 
        OR licentie6hc = @lc6 OR licentie7hc = @LC7 OR 
        licentie8hc = @lc8 OR licentie9hc = @lc9 OR licentie10hc = @lc10)";

等同于:

SELECT * FROM TABLE
WHERE id=@id AND(lic1=@lic1 OR lic2=@lic2...etc...OR lic10=@lc10);

注意:我知道您可能无法更改数据库结构,但是如您所做的那样有十列,表明您的数据库可以从某些重构中受益。

答案 1 :(得分:2)

将您的查询更改为使用OR而不是AND,并且您不必对相同的值使用不同的参数,请尝试此操作,

IDCheck.CommORText = "SELECT * FROM cloudposgebruikers WHERE id = @id OR licentie1hc = @lc OR licentie2hc = @lc"
        + " OR licentie3hc = @lc OR licentie4hc = @lc OR licentie5hc = @lc OR licentie6hc = @lc OR licentie7hc = @LC OR licentie8hc = @lc"
        + " OR licentie9hc = @lc OR licentie10hc = @lc";
        IDCheck.Parameters.AddWithValue("@id", licenseid);
        IDCheck.Parameters.AddWithValue("@lc", GetId);