c#如何实现多条件模糊查询
SqlConnection conn = new SqlConnection();conn.ConnectionString = "server=LUKE\\SQLEXPRESS;uid=sa;pwd=sa;database=luke";
conn.Open();
DataTable dt = new DataTable("resouce");
dt.Clear();
SqlDataAdapter adp = new SqlDataAdapter("select * from text1", conn);
adp.Fill(dt);
string sql = "select * from text1 where bh like %"+textBox1.Text+"%";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = command;
ad.Fill(dt);
dt.Clear();
dataGridView1.DataSource = dt.DefaultView;
我要实现的是多条件模糊查询,在执行sql语句之前,先根据条件将sql语句组合好,再进行执行类似于
if request("ckc")="查" then
if request("bh")<>""then
if cond="" then
cond="编号 like '%" & trim(request("bh")) & "%'"
else
cond = cond & " AND 编号 like '%" & trim(request("bh")) & "%'"
end if
end if
if request("ckbh")<>""then
if cond="" then
cond="出库编号 like '%" & trim(request("ckbh")) & "%'"
else
cond = cond & " AND 出库编号 like '%" & trim(request("ckbh")) & "%'"
end if
end if
if request("clmc")<>"" then
If cond="" Then
cond="材料名称 like '%" & trim(request("clmc")) & "%'"
else
cond = cond & "and 材料名称 like '%" & trim(request("clmc")) & "%'"
end if
end if
if cond<>""then
sql="select * from 出库表 where " & cond & "order by 日期"
else
sql="select * from 出库表 order by 日期"
end if