試圖從MySql數據庫執行select語句。下面的作品,當我插上直接變量:C#MySql語句。發現我的錯誤!
MySqlCommand playerSearch = conn.CreateCommand();
playerSearch.CommandText = @"select username from players where username like '%" + username + "%'";
playerSearch.Prepare();
// Execute the command, get the restuls, etc.
但是如果我試圖用paramater將做到這一點的首選方式,如:
MySqlCommand playerSearch = conn.CreateCommand();
playerSearch.CommandText = @"select username from players where username like @username";
playerSearch.Prepare();
playerSearch.Parameters.AddWtihValue("@username", "'%" + username + "%'");
// Execute the command, get the restuls, etc.
我沒有得到任何結果從查詢回來。到目前爲止,我還沒有想出爲什麼這不起作用。有什麼建議?
工作就像一個魅力,謝謝。猜測這是由於AddWithValue的情況? – vimalloc 2010-11-28 21:47:25