2012-04-04 138 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,然後在不檢查柱更簡單。否則,有一個下拉菜單,可以選擇要搜索的列。

相關問題