0

我有一个具有多个 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);

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

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

或者让我们说只有“id”和licentie1hc“匹配.. => return true

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

4

2 回答 2

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

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

于 2013-03-10T15:50:45.103 回答
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);
于 2013-03-10T15:57:02.613 回答