嘿所有我不知道爲什麼我收到此錯誤:當我嘗試運行此了條件,預計,不久的「@ VAL4」在上下文中指定非布爾類型的表達式
An expression of non-boolean type specified in a context where a condition is expected, near '@val4'
代碼在C#:
SELECT * FROM tT WHERE active = @val1 AND (ApDate BETWEEN @val2 AND @val3) AND @val4
上面查詢的數據是:
@val1 = 1
@val2 = 9/30/2016 12:00:00 AM
@val3 = 9/30/2017 12:00:00 AM
@val4 = (RequestID = 0 OR RequestID =469 OR RequestID =471 OR RequestID =472 OR
RequestID =473 OR RequestID =474 OR RequestID =494 OR
RequestID =496 OR RequestID =497)
所以把那乾脆它看起來像THI S:
SELECT *
FROM tT
WHERE active = 1
AND (ApDate BETWEEN '9/30/2016 12:00:00 AM' AND '9/30/2017 12:00:00 AM')
AND (RequestID = 0 OR RequestID =469 OR RequestID =471 OR RequestID =472 OR
RequestID =473 OR RequestID =474 OR RequestID =494 OR
RequestID =496 OR RequestID =497)
然而,當我運行的服務器管理Studio中相同的查詢它運行得很好......
而我只是用這樣的:
command.Parameters.AddWithValue("@val1", Value_here);
command.Parameters.AddWithValue("@val2", Value_here);
command.Parameters.AddWithValue("@val3", Value_here);
command.Parameters.AddWithValue("@val4", Value_here);
由於意味着將4個參數添加到查詢中。
注意不是由於當前問題,而不是現在相同的命名問題。
@DavidG它正在被比較..它正在比較** RequestID ** – StealthRT
對不起,誤讀。但是,似乎您正在嘗試將原始SQL作爲參數傳遞,是嗎?你能證明你是如何做到的嗎? – DavidG
所以你現在有答案,而且我建議http://blogs.msmvps。com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – DavidG