C# 高级搜索-url编码

C# 高级搜索-url编码,c#,asp.net,C#,Asp.net,我有一个很奇怪的问题,我不知道如何解决这个问题 我有一个预先搜索我的项目,它基本上是学校搜索 acc我的问题是我正在使用LIKE来比较选项,在搜索结束时,我的查询应该如下所示: select * from tbl_schooldetails where state = 'Gujarat' and city = 'Ahmedabad' and area = 'Navrangpura' and ( board = 'xx' or board LIKE '%CBSE Board%' o

我有一个很奇怪的问题,我不知道如何解决这个问题

我有一个预先搜索我的项目,它基本上是学校搜索

acc我的问题是我正在使用
LIKE
来比较选项,在搜索结束时,我的查询应该如下所示:

select *
from tbl_schooldetails
where
  state = 'Gujarat'
  and city = 'Ahmedabad'
  and area = 'Navrangpura'
  and ( board = 'xx' or board LIKE '%CBSE Board%' or board LIKE '%Gujarat Board%')
但我得到的是以下查询:

select *
from tbl_schooldetails
where
  state = 'Gujarat'
  and city = 'Ahmedabad'
  and area = 'Navrangpura'
  and ( board = 'xx' or board LIKE '�SE Board%' or board LIKE '%Gujarat Board%')
如果您注意到我的
%CB
已转换为“� " 签名,因此我无法搜索与“CBSE董事会”选项相关的任何结果

有人能告诉我如何摆脱这种URL编码吗

这是生成此查询的代码:

  string qry = "select * from tbl_schooldetails where state = '" + sdpd4.SelectedItem.Text + "'";

    if (sdpd2.SelectedItem.Text != "Select City")
    {
        qry += " and city = '" + sdpd2.SelectedItem.Text + "'";
    }

    if (sdpd1.SelectedItem.Text != "Select Area")
    {
        qry += " and area = '" + sdpd1.SelectedItem.Text + "'";
    }

    if (CheckBoxList3.SelectedItem != null)
    {
        qry = qry + " and ( board = 'xx'";

          for (int i = CheckBoxList3.Items.Count - 1; i >= 0; i--)
          {
              if (CheckBoxList3.Items[i].Selected == true)
              {

                  string mt = CheckBoxList3.Items[i].ToString();
                  qry = qry + " or board LIKE '" + '%' + mt + '%' + "'";

              }
          }
            qry = qry + ")";
    }


    if (RadioButtonList1.SelectedItem != null)
    {
        qry += " and gender ='" + RadioButtonList1.SelectedItem.Text + "'";
    }



    Response.Redirect("schoolsearchresult2.aspx?search=" + qry);

编辑了,因为原来的问题更清楚了

只要改变这个:

Response.Redirect("schoolsearchresult2.aspx?search=" + qry);
为此:

Response.Redirect("schoolsearchresult2.aspx?search=" 
    + HttpServerUtility.UrlEncode(qry));
…但是:我的警告(以及其他人的警告)仍然正确:在查询字符串中传递WHERE子句是非常危险的——对结果URL的细微调整可能会破坏数据库

原始答案

您似乎正在将%CB放入一个URL,该URL在服务器上被解释为十六进制数字

如果使用%25CB,则应将其解释为“%CB”

或者你可以使用一个内置的c#函数。我想你需要的是

非常重要:


如果这是一个真正的应用程序,而不是一个概念验证项目,

我建议您以这种方式构建sql查询,这样您就不必担心url编码和sql注入

string sqlQuery = "select *
from tbl_schooldetails
where
  state = @state
  and city = @city
  and area = @area
  and ( board = @board1 or board LIKE ";
 sqlQuery += "%@board2%";
sqlQuery += " or board LIKE %@board3%");
然后在执行查询时使用

e、 g


……等等

khyati,你能告诉我你是如何或从哪里得到查询的吗?你没有从URL直接传递这些参数,是吗?你实际上是如何构建和执行这些查询的?一些额外的代码会非常有用-显示你如何将查询放在一起并在数据库上运行它将是非常好的at.请注意,如果您直接从URL传递参数,您将面临SQL注入的严重风险。您是否尝试过任何方法?如URLDecode()?如前所述,通过URL传递完整查询或查询参数不是一个好主意。不,我没有使用URLDecode()…向他提供详细信息很好,但是…你为什么要使用
String.Format
来连接常量?解决方案非常有用,但我已经在大部分项目中进行了编码,所有选项都可以100%正常工作。所以我现在不想更改编码。只需要任何与解决方案相关的url encoding.sry到mark作为ans经过了很长时间,但我今天acc解决了它…哈哈哈。我正在寻找一些其他选项,但认为这是ws-d最好的…感谢所有有这个奇怪问题的人,我只是使用Server.UrlEncode(qry)传递查询字符串。
SqlCommand cmd = new SqlCommand(command);
cmd.Parameters.AddWithValue("@state", Request.Form["state"]);