2013-08-23 181 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值可以包含任何内容并且不会转义。例如,一个人可以输入'; - 从表中删除; 和删除你的表一切......

相关问题