1
我有一個表結構如下所示:C#SQL Server查詢名字/姓氏列,其中第一名稱有空間
user_id | first_name | last_name
1 | John | Smith
2 | Mary | Jones
3 | Sang He | Lau
// here is a sample of the code I currently have to do a user search
public List<string> getResults(string autocompleteQuery) {
string[] tokens = autocompleteQuery.Split(' ');
// token[0] = first name parameter (@firstName)
// token[1] = last name parameter (@lastName)
// code here executes the following query:
// SELECT * FROM [user] WHERE first_name = @firstName AND last_name = @lastName
}
我在這裏的問題是,它使用一個空格作爲列表分隔符聯合下面的代碼如果user_id = 3中顯示的名字中有空格,則查詢不起作用。
如何優雅地升級代碼或查詢以處理此情況?謝謝。
您應該考慮名字,如first_name =「Maria de los Angeles」last_name =「Cruz Ortiz」。 – hatchet