2013-03-10 75 views
0

我已經有多個addwithvalue查詢值Mysql的多個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」參數相匹配的一個...

回答

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 [email protected] AND([email protected] OR [email protected] [email protected]); 

注意:我知道您可能無法更改您的數據庫結構,但您有10列,表明您的數據庫可能從一些重構中受益。

+0

如果我有足夠代表我會送給你一個+1快速和良好的答案..謝謝! – DeMama 2013-03-10 15:57:38

+0

thx!查看rs答案,他發現了一些我沒有大大簡化代碼的東西。 – 2013-03-10 15:58:25

+0

呵呵不,我們不能改變我們的數據庫結構,這是我們的許可證制度的一部分,所以沒有其他選擇..但無論如何感謝:D – DeMama 2013-03-11 19:46:12

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

好主意!謝謝 – DeMama 2013-03-10 16:01:08