查询时根据你所输入的内容进行查询
str_query1 = "select * from table";
string key1 = "";
string key2 = "";
string key3 = "";
string key4 = ""; //有几个条件就加几个key,我这里有四个
if (textBox1.Text.Trim() != "")
key1 = " column1 like '%" + textBox1.Text.Trim() + "%'";
string key1 = "";
string key2 = "";
string key3 = "";
string key4 = ""; //有几个条件就加几个key,我这里有四个
if (textBox1.Text.Trim() != "")
key1 = " column1 like '%" + textBox1.Text.Trim() + "%'";
if (textBox2.Text.Trim() != "")
key2 = " column2 like '%" + textBox2.Text.Trim() + "%'";
key2 = " column2 like '%" + textBox2.Text.Trim() + "%'";
if (textBox3.Text.Trim() != "")
key3 = " column3 like '%" + textBox3.Text.Trim() + "%'";
if (textBox4.Text.Trim() != "")
key4 = " column4 like '%" + textBox4.Text.Trim() + "%'"; //这也一样
key3 = " column3 like '%" + textBox3.Text.Trim() + "%'";
if (textBox4.Text.Trim() != "")
key4 = " column4 like '%" + textBox4.Text.Trim() + "%'"; //这也一样
if (key1 != "" || key2 != "" || key3 != "" || key4 != "" )
{
str_query1 = "select * from table where";
if (key1 != "") //这些条件是个递增的过程,有几个条件就加几个if语句
{
str_query1 += key1;
}
{
str_query1 = "select * from table where";
if (key1 != "") //这些条件是个递增的过程,有几个条件就加几个if语句
{
str_query1 += key1;
}
if (key1 != "" && key2 != "")
{
str_query1 += "and" + key2;
}
else
{
str_query1 += key2;
}
{
str_query1 += "and" + key2;
}
else
{
str_query1 += key2;
}
else
{
str_query1 += key2;
}
{
str_query1 += key2;
}
if ((key1 != "" || key2 != "") && key3 != "")
{
str_query1 += "and" + key3;
}
else
{
str_query1 += key3;
}
{
str_query1 += "and" + key3;
}
else
{
str_query1 += key3;
}
if ((key1 != "" || key2 != "" || key3 != "") && key4 != "") //后面还可以加key5,相信你看到这里已经名白怎么加了
{
str_query1 += "and" + key4;
}
else
{
str_query1 += key4;
}
}
{
str_query1 += "and" + key4;
}
else
{
str_query1 += key4;
}
}
转载于:https://blog.51cto.com/514143/270158