我試圖將參數傳遞給quert,但它不起作用。什麼是最好的方式來做到這一點?我不想連接字符串。這裏是我的查詢:使用LIKE的sql參數化查詢
string cmd = @"
SELECT
*
FROM
TABLE
WHERE
p.PromptTypeID = pt.ID
AND p.PromptDomainID = pd.ID
AND p.LanguageID = pl.ID
AND p.VoiceID = pv.ID
AND p.Active='Y'
AND PromptText LIKE ?
ORDER BY
p.ID DESC";
using (SqlCommand command = new SqlCommand())
{
command.CommandText = cmd;
command.Transaction = transac;
command.Connection = cnn;
command.Parameters.Add("?", SqlDbType.VarChar, 50).Value = "%" + text + "%";
using (SqlDataAdapter adp = new SqlDataAdapter(command))
{
adp.Fill(dt);
}
}
我無法通過LIKE運算符傳遞值。我也嘗試使用@text而不是「?」但它不起作用。有什麼建議麼?
順便說一句,它給''附近的語法不正確。 SQLEXCEPTION
你正在收到什麼錯誤? –
看起來你至少錯過了類似值的SQL中的文本撇號。應該適當成爲參數值的一部分。 –
@GordonLinoff'?'附近語法不正確 – cihata87