2012-07-26 123 views
3

我有非常奇怪的問題,我不知道如何擺脫這一點。高級搜索 - 網址編碼

我有我的項目,它基本上學校搜索

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); 
+0

KHYATI而不是請你能告訴我如何或你在哪裏得到的查詢 – HatSoft 2012-07-26 21:46:28

+2

你是不是從URL傳遞這些參數正確的是你嗎?你是如何建立和執行這些查詢的?一些額外的代碼會非常有幫助 - 顯示如何將查詢放在一起並在數據庫上運行它會很好。請注意,如果您從URL直接傳遞參數,那麼存在SQL注入的嚴重風險。 – dash 2012-07-26 21:47:22

+0

你嘗試過什麼嗎?像URLDecode()? 正如前面提到的,通過URL傳遞完整的查詢或查詢參數並不是一個好主意。 – 2012-07-26 21:49:49

回答

4

現在,原來的問題是清晰的編輯

只要改變這一點:

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

要這樣:

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

......但是:我的警告(和其他人的)仍然是正確的:通過在WHERE子句中的查詢字符串是非常危險 - 由此產生的URL的微小調整可以銷燬您的數據庫。

原來的答覆

你似乎把%CB成URL,這是該服務器作爲一個十六進制數字的解釋。

如果您使用%25CB,則應將其解釋爲「%CB」。

或者,您可以使用其中一個內置的c#函數。我認爲你需要的是HttpServerUtility.UrlEncode

非常重要:

如果這是一個真正的應用程序,而不是證明概念的一個項目,you must not copy data directly from the URL into your SQL string!

+0

+1對於鏈接! LOL – billy 2012-07-26 22:00:41

+0

sry在soooo後標記爲ans,但我今天解決了它。 。 。哈哈哈。我正在尋找一些其他的選擇,但認爲這是最好的。 。 。非常感謝 – jackerj 2012-08-18 10:29:22

+0

對於有這個奇怪問題的人來說,我只是使用Server.UrlEncode(qry)來傳遞查詢字符串。 – jackerj 2012-08-18 10:37:01

0

我建議你建立你的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%"); 

,然後當執行查詢使用

例如

SqlCommand cmd = new SqlCommand(command); 
cmd.Parameters.AddWithValue("@state", Request.Form["state"]); 

......等等

+0

給他提供細節很好,但是......爲什麼你用'String.Format'連接常量? – egrunin 2012-07-26 22:03:29

+0

@egrunin好點壞現在改變它 – HatSoft 2012-07-26 22:07:48

+0

雅thx很多的解決方案,但我已經做了大部分的項目編碼和它的工作100%罰款的所有選項。所以我現在不考慮改變編碼。只需要任何解決方案相關的url編碼。 – jackerj 2012-07-26 22:13:48