gpt4 book ai didi

c# - 高级搜索 - url 编码

转载 作者:行者123 更新时间:2023-11-30 15:38:14 25 4
gpt4 key购买 nike

我有一个很奇怪的问题,我不知道如何摆脱它。

我有一个针对我的项目的高级搜索,它基本上是学校搜索

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 Board”选项相关的任何结果。

谁能告诉我如何摆脱这种 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# 函数之一。我想你需要的是 HttpServerUtility.UrlEncode .

非常重要:

如果这是一个真实的应用程序,而不是概念验证项目,you must not copy data directly from the URL into your SQL string!

关于c# - 高级搜索 - url 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11678422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com