C#.NET 模糊组合查询的例子
{
if (Sender.text.length>0)
{
WhereText +=" SenderName like '%"+ Sender +"%' and";
if (Sender.text.length>0)
{
WhereText +=" SenderName like '%"+ Sender +"%' and";
}
if (Receive.text.length>0)
{
WhereText +=" AddresseeName like '%"+ Receive +"%' and";
WhereText +=" AddresseeName like '%"+ Receive +"%' and";
}
if (DeleteTime.text.length>0)
if (DeleteTime.text.length>0)
{
WhereText +=" convert(varchar(10),DeleteTime,120) ='"+ DeleteTime +"' and";
WhereText +=" convert(varchar(10),DeleteTime,120) ='"+ DeleteTime +"' and";
{
cmdText += WhereText.Substring(0, WhereText.Length -4);//去掉最后一个and
}
cmdText += WhereText.Substring(0, WhereText.Length -4);//去掉最后一个and
}
===============================================

1 //查询
2 protectedvoid btn_cx_Click(object sender, EventArgs e)
3 {
4 string WhereText =" where";
5 string cmdText ="select * from htjbxxb";
6 if (txt_htbh.Text.Length >0)
7 {
8 WhereText +=" htbh='"+ txt_htbh.Text.Trim() +"' and";
9 }
10 if (txt_htmc.Text.Length >0)
11 {
12 WhereText +=" htmc like '%"+ txt_htmc.Text.Trim() +"%' and";
13 }
14 if (txt_wgrq.Text.Length >0)
15 {
16 WhereText +=" wgrq= '"+ txt_wgrq.Text.Trim() +"' and";
17 }
18 if (txt_jfdw.Text.Length >0)
19 {
20 WhereText +=" jfdw like '%"+ txt_jfdw.Text.Trim() +"%' and";
21 }
22 if (txt_qdrqdy.Text.Length >0)
23 {
24 WhereText +=" qdrq>='"+ txt_qdrqdy.Text.Trim() +"' and";
25 }
26 if (txt_qdrqxy.Text.Length >0)
27 {
28 WhereText +=" qdrq<'"+ txt_qdrqxy.Text.Trim() +"' and";
29 }
30 if (ddl_xmlx.SelectedValue.Length >0)
31 {
32 WhereText +=" xmlx='"+ ddl_cdbm.SelectedValue +"' and";
33 }
34 if (txt_htjedy.Text.Length >0)
35 {
36 WhereText +=" htje>='"+ txt_htjedy.Text.Trim() +"' and";
37 }
38 if (txt_htjexy.Text.Length >0)
39 {
40 WhereText +=" htje<'"+ txt_htjexy.Text.Trim() +"' and";
41 }
42 if (rbl_xmgm.SelectedValue.ToString().Length >0)
43 {
44 WhereText +=" xmgm='"+ rbl_xmgm.SelectedValue +"' and";
45 }
46 if (ddl_khjl.SelectedValue !="0")
47 {
48 WhereText +=" khjl='"+ ddl_khjl.SelectedValue +"' and";
49 }
50 if (ddl_cdbm.SelectedValue !="0")
51 {
52 WhereText +=" cdbm='"+ ddl_cdbm.SelectedValue +"' and";
53 }
54 if (ddl_xmjl.SelectedValue !="0"&& ddl_xmjl.SelectedValue !="")
55 {
56 WhereText +=" xmjl='"+ ddl_xmjl.SelectedValue +"' and";
57 }
58
59 if (WhereText.Length>0)
60 {
61 cmdText += WhereText.Substring(0, WhereText.Length -4);//去掉最后一个and
62
63 }
64
65 string strCon ="server=.;database=tangge;uid=sa;pwd=sa";
66 SqlConnection conn =new SqlConnection(strCon);
67 conn.Open();
68 DataSet ds =new DataSet();
69 SqlCommand cmd =new SqlCommand(cmdText, conn);//cmdText是sql语句,con是连接数据库
70 SqlDataAdapter da =new SqlDataAdapter(cmd);
71 da.Fill(ds);
72 GridView1.DataSource = ds;
73 GridView1.DataBind();
74 conn.Close();
75
76 }
77
2 protectedvoid btn_cx_Click(object sender, EventArgs e)
3 {
4 string WhereText =" where";
5 string cmdText ="select * from htjbxxb";
6 if (txt_htbh.Text.Length >0)
7 {
8 WhereText +=" htbh='"+ txt_htbh.Text.Trim() +"' and";
9 }
10 if (txt_htmc.Text.Length >0)
11 {
12 WhereText +=" htmc like '%"+ txt_htmc.Text.Trim() +"%' and";
13 }
14 if (txt_wgrq.Text.Length >0)
15 {
16 WhereText +=" wgrq= '"+ txt_wgrq.Text.Trim() +"' and";
17 }
18 if (txt_jfdw.Text.Length >0)
19 {
20 WhereText +=" jfdw like '%"+ txt_jfdw.Text.Trim() +"%' and";
21 }
22 if (txt_qdrqdy.Text.Length >0)
23 {
24 WhereText +=" qdrq>='"+ txt_qdrqdy.Text.Trim() +"' and";
25 }
26 if (txt_qdrqxy.Text.Length >0)
27 {
28 WhereText +=" qdrq<'"+ txt_qdrqxy.Text.Trim() +"' and";
29 }
30 if (ddl_xmlx.SelectedValue.Length >0)
31 {
32 WhereText +=" xmlx='"+ ddl_cdbm.SelectedValue +"' and";
33 }
34 if (txt_htjedy.Text.Length >0)
35 {
36 WhereText +=" htje>='"+ txt_htjedy.Text.Trim() +"' and";
37 }
38 if (txt_htjexy.Text.Length >0)
39 {
40 WhereText +=" htje<'"+ txt_htjexy.Text.Trim() +"' and";
41 }
42 if (rbl_xmgm.SelectedValue.ToString().Length >0)
43 {
44 WhereText +=" xmgm='"+ rbl_xmgm.SelectedValue +"' and";
45 }
46 if (ddl_khjl.SelectedValue !="0")
47 {
48 WhereText +=" khjl='"+ ddl_khjl.SelectedValue +"' and";
49 }
50 if (ddl_cdbm.SelectedValue !="0")
51 {
52 WhereText +=" cdbm='"+ ddl_cdbm.SelectedValue +"' and";
53 }
54 if (ddl_xmjl.SelectedValue !="0"&& ddl_xmjl.SelectedValue !="")
55 {
56 WhereText +=" xmjl='"+ ddl_xmjl.SelectedValue +"' and";
57 }
58
59 if (WhereText.Length>0)
60 {
61 cmdText += WhereText.Substring(0, WhereText.Length -4);//去掉最后一个and
62
63 }
64
65 string strCon ="server=.;database=tangge;uid=sa;pwd=sa";
66 SqlConnection conn =new SqlConnection(strCon);
67 conn.Open();
68 DataSet ds =new DataSet();
69 SqlCommand cmd =new SqlCommand(cmdText, conn);//cmdText是sql语句,con是连接数据库
70 SqlDataAdapter da =new SqlDataAdapter(cmd);
71 da.Fill(ds);
72 GridView1.DataSource = ds;
73 GridView1.DataBind();
74 conn.Close();
75
76 }
77
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2010/11/13/1876188.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具