【发布时间】:2017-06-10 13:26:27
【问题描述】:
当我在我的 TextBox 中键入 ' 时,会导致错误。我知道这是因为' 是 SQL 查询的一部分。我该如何避免呢?我已经用 Parameter 完成了,但甚至没有工作。
private void theFilter(string FilterValue) {
string thisQuery = "SELECT * FROM [Customer] WHERE CONCAT([Name], [Address], [Discount]) LIKE '%" + @FilterValue + "%'";
using(SqlConnection thisSqlConnection = new SqlConnection(theConnectionString))
using(SqlCommand thisSqlCommand = new SqlCommand(thisQuery, thisSqlConnection)) {
thisSqlCommand.Parameters.AddWithValue("@FilterValue", FilterValue);
using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
using(DataTable thisDataTable = new DataTable()) {
thisSqlDataAdapter.Fill(thisDataTable);
DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
}
}
}
【问题讨论】:
-
验证您的文本框。
-
大声笑任何人都可以做到。
-
不,我不想要那个。
-
那么问题出在哪里,你不是来自任何人?
-
哈!看下面,他们得到了答案!
标签: c# sql-server wpf