How to search int or string in all fields

Go To StackoverFlow.com

0

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

How to search int or string in all fields?

Edit code:

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); 
} 
2012-04-04 17:38
by Kazim King
Don't write code like this, it is subject to SQL injection attack - RedFilter 2012-04-04 17:40
if (comboBox1.Text == "RegistrationId") { da = new SqlDataAdapter("SELECT * FROM Patient Where RegistrationId = '" + textBox1.Text + "'", cn);

            }
            else if (comboBox1.Text == "Patient_Name")
            {
                da = new SqlDataAdapter("SELECT * FROM Patient Where Patient_Name = '" + textBox1.Text + "'", cn);
            }
< - Kazim King 2012-04-04 18:13


0

For one that method can be subject to SQL injection attack so you need to sanitize it. Otherwise one solution (after you check for attacks) is:

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

This can be made simpler by the fact that if they don't know the id of the item then don't check that column. Otherwise have a dropdown that chooses which column they are searching by.

2012-04-04 20:13
by Kyra
Ads