【问题标题】:Parameter act the same as regular string参数作用与普通字符串相同
【发布时间】: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


【解决方案1】:

试试这个

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;
        }
    }
}

请记住,您在参数中使用字符串,而不是变量。您必须在 AddWithValue 方法中指明“%”才能在查询中使用通配符。

【讨论】:

  • 不,它不起作用。同LIKE '% 'String' %'
  • 您传递来测试这个@LemBidi 的FilterValue 的值是多少?因为这段代码应该工作。
  • @ahmedab​​delqader 这是一个巧合,冷静下来。
  • @LemBidi,不.. mjwills 是第一个男人 :) 选择你想要的形式被接受的答案,只是我要求你尽可能公平,谢谢。
【解决方案2】:
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", "%" + SqlLikeEscape(FilterValue) + "%");
        using(SqlDataAdapter thisSqlDataAdapter = new SqlDataAdapter(thisSqlCommand))
        using(DataTable thisDataTable = new DataTable()) {
            thisSqlDataAdapter.Fill(thisDataTable);
            DataGrid_Customer.ItemsSource = thisDataTable.DefaultView;
        }
    }
}

// This function is important, since otherwise if there is a % in the table (or other special LIKE characters) then it is hard to search for them
//see https://stackoverflow.com/questions/18693349/how-do-i-find-with-the-like-operator-in-sql-server
public static string SqlLikeEscape(string value)
{
    if (string.IsNullOrEmpty(value)) return value;
    return Regex.Replace(value, @"(?<ch>%|_|\[)", @"[${ch}]");
}

【讨论】:

  • 抱歉,他的“编辑”历史比你的“创建评论”更早
  • 没问题!我已经添加了我的 SqlLikeEscape 函数——希望对你有用!
  • 我给你+1
  • 谢谢,这很有用,防止进一步的错误。
猜你喜欢
  • 2010-11-27
  • 2021-03-30
  • 2021-12-17
  • 1970-01-01
  • 2012-12-16
  • 2018-01-08
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
相关资源
最近更新 更多