2013-08-23 180 views
0
public string[] ResultsQuery; 
    public int i; 
    public string criteria; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 


     string connString = @"Data Source=ITLAPTOP\SQLEXPRESS;Initial Catalog=trial;Integrated Security=True"; 
     SqlConnection connStudent = new SqlConnection(connString); 
     connStudent.Open(); 

     if (Request.QueryString[TextBox1.Text] != null) 
     { 
      ResultsQuery = Request.QueryString[TextBox1.Text].Split(' '); 


      foreach (string textbox1 in ResultsQuery) 
      { 
       if (!string.IsNullOrEmpty(criteria)) 
        criteria += " OR "; 

       criteria += "SearchName LIKE '%" + textbox1 + "%' "; 
      } 

      string SqlInsertStatement = @"select * from trial.dbo.Student where Student.SearchName where '" + criteria; 
      SqlCommand cmdTxt = new SqlCommand(SqlInsertStatement, connStudent); 
      SqlDataReader dtrACode = cmdTxt.ExecuteReader(); 
      dtrACode.Read(); 


      try 
      { 
       if ((dtrACode["SearchName"].ToString().Trim().Length != 0)) 
       { 

       } 
       ListBox1.Items.Add(dtrACode["SearchName"].ToString()); 
      } 
      catch (Exception) 
      { 
       ListBox1.Items.Add("NO RECORD FOUND!"); 


      } 


      connStudent.Close(); 
      connStudent.Dispose(); 
     } 
    } 

我想要顯示的關鍵字的用戶輸入的所有發生的所有可能發生,例如 列表數據庫: abCARdfg CARsdg CAR dfgsd sdkgs我要搜索的用戶輸入的內容數據庫,並顯示單詞

==當我搜索字車應顯示所有與汽車的字符串和dfgsd,sdkgs將不會顯示

查詢工作,就像我在EXP讓SQL服務器顯示,但我不知道在哪裏把它放在C#中的代碼,當我點擊按鈕時,它不顯示任何東西,即使沒有記錄發現作爲錯誤處理程序

+0

調試你的代碼,並把就行'串SqlInsertStatement'一個破發點,然後單步運行線和捕捉變量'SqlInsertStatement'值。將該查詢複製並粘貼到SQL Server Management Studio並運行它,我打賭你會得到一個錯誤。最後,在您的問題和幫助中發佈該查詢。 :-) –

+1

希望沒有人在文本框中鍵入'%'DROP DATABASE試用版--': –

+0

我試圖調試它並放置了幾個斷點,它只到達這一行「if(Request.QueryString [TextBox1.Text] != null)「並停止 – deniel

回答

2

因爲我沒有訪問到您的代碼,我最好的猜測是,你必須使用以下行錯誤:

string SqlInsertStatement = @"select * from trial.dbo.Student where Student.SearchName where '" + criteria; 

替換下列要求:

string SqlInsertStatement = @"select * from trial.dbo.Student where " + criteria; 
+0

仍然是一樣的:( – deniel

+0

嘗試在'string SqlInsertStatement'這一行添加一個斷點,看看有什麼SQL正在執行,我懷疑它沒有選擇你的搜索詞 - 例如CAR – skub

+0

theres沒有,是的,我也認爲它沒有拿起搜索詞 – deniel

0

刪除嘗試/捕獲併發布拋出的Exception的細節。您的查詢中肯定存在錯誤。例如,你重複一個「在哪裏」。

where Student.SearchName where 

此外,您的代碼具有SQL注入漏洞,因爲textbox1值可以包含任何內容並且不會轉義。例如,一個人可以輸入'; - 從表中刪除; 和刪除你的表一切......

相關問題