【问题标题】:asp.net - searching for a data containing an apostrophe in the databaseasp.net - 在数据库中搜索包含撇号的数据
【发布时间】:2023-03-31 16:51:01
【问题描述】:

美好的一天!我很难解决这个问题。我一直在寻找这个答案,但似乎很难找到最合适的答案。

我使用此查询根据用户在 txtSearchRP 文本框中输入的内容来搜索租户的名称,它非常适用于其中没有撇号的数据,但是当用户搜索包含 ' 的名称时,它不会运行良好。

示例:用户输入 MAX'S 以搜索 MAX'S RESTAURANT

SELECT * from tenant WHERE (name LIKE '%" + txtSearchRP.Text + "%')

提前感谢您的帮助!


编辑以获取更多信息:

我实际上是在用户单击按钮后将查询传递给 sqlDataSource 以自动绑定 gridview。

SqlDataSource3.SelectCommand = SELECT * from tenant WHERE (name LIKE '%" + txtSearchRP.Text + "%')

【问题讨论】:

  • 只需将 MAX'S 中的单引号替换为两个单引号,即 name LIKE '%MAX''S%'
  • 感谢回复,请注意点赞操作符必须基于搜索框(用户输入)-txtSearcRP.Text。如何使用您提出的答案修改 (name LIKE '%" + txtSearchRP.Text + "%')?
  • 使用String的Replace方法
  • 使用 NMK 的解决方案,因为它是免费的 SQL 注入
  • 使用注入法,还有其他可能解决吗?

标签: c# sql asp.net


【解决方案1】:

试试这个

conn =  new 
    SqlConnection("ConnectionString");
conn.Open();

SqlCommand cmd = new SqlCommand(
    "SELECT * from tenant WHERE (name LIKE @tenant)", conn);
SqlParameter param  = new SqlParameter();
param.ParameterName = "@tenant";
param.Value         = "%" + txtSearchRP.Text + "%"; // you can use any wildcard operator 
cmd.Parameters.Add(param);
SqlDataReader reader = cmd.ExecuteReader();

【讨论】:

  • 那将是一个 sql 语法错误。 %@tenant% 不是合法语法。
  • 那么您可能可以将通配符运算符作为值的一部分附加。
  • 有没有其他方法可以解决使用注入的问题?不过谢谢你,我会尝试你的答案并更新你的结果。
【解决方案2】:

除了已经给出的答案,在某些应用程序中,您可能需要在用户提供的输入字符串中考虑escaping wildcards such as %

例如,如果用户输入“25%”,则匹配“%25%%”将返回包含“25”的值,而不是限制为包含“25%”的值。

您可以按如下方式转义通配符(对于 SQL Server):

 string value = ... value entered by user;
 value = value.Replace("[", "[[]");
 value = value.Replace("_", "[_]");
 value = value.Replace("%", "[%]");

【讨论】:

    【解决方案3】:

    创建存储过程的更好方法

    SP:

    Create proc sp_Search( @txtSearch nvarchar(150)) 
    as begin
      SELECT * from tenant WHERE name like @txtSearch+'%'
    end
    

    背后的代码:

     string txtSearch = txtSearchRP.Text;
            SqlDataReader dr;
            using (SqlConnection conn = new SqlConnection(cn.ConnectionString))
            {
                using (SqlCommand cmdd = new SqlCommand())
                {
                    cmdd.CommandType = CommandType.StoredProcedure;
                    cmdd.CommandText = "sp_Search";
                    cmdd.Parameters.AddWithValue("@txtSearch", txtSearch);
                    cmdd.Connection = conn;
                    conn.Open();
                    dr = cmdd.ExecuteReader(CommandBehavior.CloseConnection);
    
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            var name = dr["name"].ToString();
                            var location = dr["location"].ToString();
                        }
                    } dr.Close();
                    conn.Close();
    
                }
            }
    

    更新: 编写一个返回数据表的函数,以便我们可以将它绑定到我们的 gridview 控件,就像我在下面的代码中所做的那样

     public DataTable bindGridView() 
        {
         string txtSearch = txtSearchRP.Text;
         DataTable dt = new DataTable();
         using (SqlConnection con = new SqlConnection(cn.ConnectionString))
            {
                SqlCommand cmdd = new SqlCommand();
                cmdd.CommandType = CommandType.StoredProcedure;
                cmdd.CommandText = "sp_Search";
                 cmdd.Parameters.AddWithValue("@txtSearch", txtSearch);
                cmdd.Connection = con;
                con.Open();
                SqlDataAdapter dap = new SqlDataAdapter(cmdd);
                DataSet ds = new DataSet();
                dap.Fill(ds);
                dt = ds.Tables[0];
                con.Close();
    
            }
            return dt;
        }
    

    按钮点击:调用bindGridView()函数绑定Gridview控件

    GridView1.DataSource = bindGridView();
    GridView1.DataBind();
    

    【讨论】:

    • 谢谢你,对不起这个问题,但是我如何在点击按钮后将它传递给 sqldatasource 来填充 gridview?
    • 我包括以下几行:SqlDataSource3.SelectCommand = "SELECT * from tenant WHERE (name LIKE '%" + name +"%'); ModalPopupExtenderRP.Show(); 在 if - while 语句中测试名称变量,当我在关键字搜索中包含撇号时,它仍然没有读取或给出过滤结果,但它与纯字符串关键字很好地配合。
    • 非常感谢这个例子一直在尝试这个,但我不能只执行我想做的事情,谢谢你的努力:)
    【解决方案4】:

    感谢所有分享知识和努力的人,终于通过字符串替换方法得到了答案

    这是三行代码

    string value = txtSearchRP.Text;
    value = value.Replace("'", "['']");
    sqlDataSource3.SelectCommand = "SELECT * from tenant WHERE  (name LIKE '%" + value.ToString() +"%')";
    

    通过共同努力,您在此处发布的答案,我们以最简单的形式解决了问题:)

    【讨论】:

    • 您应该使用 NMK 节目之类的参数化语句,并仔细阅读人们说 SQL injection 时的意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2011-03-11
    • 2019-03-24
    相关资源
    最近更新 更多