3 回答

TA貢獻1111條經(jīng)驗 獲得超0個贊
使用like operator
而不是=
SELECT * FROM Customers WHERE CompanyName like @filter + '%'"

TA貢獻1860條經(jīng)驗 獲得超9個贊
'Like' 應(yīng)該用作(SELECT * FROM Customers WHERE CompanyName LIKE @filter + '%'):
private void DoFilter()
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers WHERE CompanyName LIKE @filter + '%'", connection))
{
adapter.SelectCommand.Parameters.AddWithValue("@filter", txtFilter.Text.Trim());
DataTable TCustomers = new DataTable();
adapter.Fill(TCustomers);
lstCustomers.DisplayMember = "CompanyName";
lstCustomers.ValueMember = "Id";
lstCustomers.DataSource = TCustomers;
}
}

TA貢獻1799條經(jīng)驗 獲得超6個贊
您不是在使用LIKE
運算符,而是在使用=
運算符。
它應(yīng)該是:
using (SqlDataAdapter adapter = new SqlDataAdapter( "SELECT * FROM Customers WHERE CompanyName LIKE @filter + '%'", connection))
按照您編寫它的方式,SQL 將匹配一個末尾帶有 a 的精確字符串%
,因此您不會得到任何結(jié)果。
- 3 回答
- 0 關(guān)注
- 188 瀏覽
添加回答
舉報