如何在搜索文本框中搜索我的ID,中间名,姓氏

时间:2017-08-23 17:00:41

标签: c# database forms

如何在搜索文本框中搜索我的id,middlename,lastname?我不能使用WHERE CONCAT(),因为我的数据库只是微软Visual Studio 2010内置的

    private void textBox14_TextChanged(object sender, EventArgs e)
    {                       
        if (string.IsNullOrEmpty(textBox14.Text))
        {
            SqlDataAdapter sda = new SqlDataAdapter("Select * from table1", con);
            DataTable data = new DataTable();
            sda.Fill(data);
            dataGridView1.DataSource = data;
        }
        else
        {
            SqlDataAdapter sda = new SqlDataAdapter("SELECT id, FirstName, MiddleName, LastName, Gender, DateofBirth, PlaceofBirth, Address, FathersName, FathersOccupation, MothersName, MothersOccupation, Guardian, Relation, GuardianOccupation, image FROM table1 WHERE FirstName LIKE'" + textBox14.Text + "%'", con);
            DataTable data = new DataTable();
            sda.Fill(data);
            dataGridView1.DataSource = data;
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以对要从文本框中搜索的多个内容使用OR。试试这个:

private void textBox14_TextChanged(object sender, EventArgs e)
{


    if (string.IsNullOrEmpty(textBox14.Text))
    {
        SqlDataAdapter sda = new SqlDataAdapter("Select * from table1", con);
        DataTable data = new DataTable();
        sda.Fill(data);
        dataGridView1.DataSource = data;
    }

    else
    {
        SqlDataAdapter sda = new SqlDataAdapter("SELECT id, FirstName, MiddleName, LastName, Gender, DateofBirth, PlaceofBirth, Address, FathersName, FathersOccupation, MothersName, MothersOccupation, Guardian, Relation, GuardianOccupation, image FROM table1 WHERE FirstName LIKE'" + textBox14.Text + "%' OR id LIKE'" + textBox14.Text + "%' OR MiddleName LIKE'" + textBox14.Text + "%' OR LastName LIKE'" + textBox14.Text + "%'", con);
        DataTable data = new DataTable();
        sda.Fill(data);
        dataGridView1.DataSource = data;
    }



}