2012-04-04 160 views
0
SqlDataAdapter da = 
    new SqlDataAdapter("SELECT * 
     FROM Patient 
     Where Registration_Id = '" + textBox1.Text + "' 
      OR Patient_Name = '" + textBox1.Text + "'", cn); 

如何在所有字段中搜索int或字符串?如何在所有字段中搜索int或字符串

编辑代码:

if (comboBox1.Text == "Registration_Id") 
{ 
    da = new SqlDataAdapter("SELECT * 
          FROM Patient 
          Where Registration_Id = '" + textBox1.Text + "'", cn); 
} 
else if (comboBox1.Text == "Patient_Name") 
{ 
    da = new SqlDataAdapter("SELECT * 
          FROM Patient 
          Where Patient_Name = '" + textBox1.Text + "'", cn); 
} 
+7

不要这样写代码,它会受到SQL注入攻击。 – RedFilter 2012-04-04 17:40:11

+0

if(comboBox1.Text ==“Registration_Id”) da = new SqlDataAdapter(“SELECT * FROM Patient Where Registration_Id ='”+ textBox1.Text +“'”,cn); } 否则如果(comboBox1.Text == “Patient_Name”) { DA =新的SqlDataAdapter( “SELECT * FROM患者在哪里Patient_Name = '” + textBox1.Text + “'”,CN); } – 2012-04-04 18:13:25

回答

0

对于一个,所以你需要清理它的方法可能受到SQL注入攻击。否则,一个解决方案(您检查事件后)为:

SELECT * 
FROM Patient 
Where column1 like "'%" + textBox1.Text + "%'" 
    or column2 like "'%" + textBox1.Text + "%'" 
    ...... 
    or columnn like "'%" + textBox1.Text + "%'" 

这可以通过一个事实,即如果他们不知道该项目的ID,然后在不检查柱更简单。否则,有一个下拉菜单,可以选择要搜索的列。

相关问题