如何在所有字段中搜索int或string

时间:2012-04-04 17:38:02

标签: sql search

SqlDataAdapter da = 
    new SqlDataAdapter("SELECT * 
        FROM Patient 
        Where Registration_Id = '" + textBox1.Text + "'  
           OR Patient_Name = '" + textBox1.Text + "'", cn);

如何在所有字段中搜索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); 
} 

1 个答案:

答案 0 :(得分:0)

对于一个方法可能会受到SQL注入攻击,因此您需要对其进行清理。否则,一个解决方案(检查攻击后)是:

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

这可以通过以下事实变得更简单:如果他们不知道项目的ID,则不检查该列。否则有一个下拉列表,选择他们搜索的列。