我還沒有完全理解你的代碼,但乍一看我說:
你需要圍繞你的命令文本括號,你需要添加OR邏輯運算符
command.CommandText += String.Format(" (userUsername LIKE @{0} OR userName LIKE @{0} OR userSurname LIKE @{0}) OR ", id);
當然
從循環中退出,你需要刪除的最終或
command.CommandText = command.CommandText.Substring(0, command.CommandText.Length - 4);
我將嘗試使用一個簡單的字符串
string sqlText = "SELECT * FROM tbl_Users WHERE userActive = 1";
if (!String.Empty.Equals(tboxInputUsers.Text))
{
// It's important to add an open parenthesis here to get a correct logic
// All activeusers with names like words
sqlText += " AND (";
string[] words = tboxInputUsers.Text.Split(' ');
string id = "id";
foreach (string word in words)
{
id += "a";
sqlText += String.Format(" (userUsername LIKE @{0} OR " +
"userName LIKE @{0} OR " +
"userSurname LIKE @{0}) OR ", id);
command.Parameters.AddWithValue(String.Format("@{0}", id), word);
}
// We are sure that there is at least one word, so we can remove the excess OR and close
// the opening parenthesis following the AND
sqlText = sqlText(0, sqlText.Length - 4);
sqlText += ")";
}
command.CommandText = sqlText;
是的,這非常有幫助!非常感謝! – miller