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」のみが一致するとしましょう.. => true を返す

これは可能ですか?別のクエリを使用できることはわかっていますが、「id」と「licentie x hc」パラメーターのいずれかが一致する必要があります...

4

2 に答える 2

2

AND の代わりに OR を使用するようにクエリを変更すると、同じ値に対して異なるパラメーターを使用する必要がなくなります。これを試してください。

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 に答える
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);

注: おそらくデータベース構造を変更できないことは理解していますが、10 個の列があるということは、データベースがリファクタリングの恩恵を受ける可能性があることを示しています。

于 2013-03-10T15:50:45.103 に答える