#region 多条件搜索时,使用List集合来拼接条件(拼接Sql)

StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" ptypeid=" + cboGroup.Text.Split('|')[0]);
            }

if (txtSearchName.Text.Trim().Length > 0)
            {
                 wheres.Add(" pname like '%" + txtSearchName.Text.Trim() + "%'");
            }

if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                 wheres.Add(" pcellphone like '%" + txtSearchCellPhone.Text.Trim() + "%'");
            }

//判断用户是否选择了条件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }
            #endregion

#region 多条件搜索使用带参数的sql语句

StringBuilder sql = new StringBuilder("select * from PhoneNum");
            List<string> wheres = new List<string>();
            List<SqlParameter> listParameter = new List<SqlParameter>();

if (cboGroup.SelectedIndex != 0)
            {
                wheres.Add(" ptypeid=@typeid ");
                listParameter.Add(new SqlParameter("@typeid", cboGroup.Text.Split('|')[0]));
            }

if (txtSearchName.Text.Trim().Length > 0)
            {
                wheres.Add(" pname like @pname ");
                //pname like '%乔%'
                //pname liek '%'+@pname+'%'
                listParameter.Add(new SqlParameter("@pname", "%" + txtSearchName.Text.Trim() + "%"));
            }

if (txtSearchCellPhone.Text.Trim().Length > 0)
            {
                wheres.Add(" pcellphone like @cellphone ");
                listParameter.Add(new SqlParameter("@cellphone", "%" + txtSearchCellPhone.Text.Trim() + "%"));
            }

//判断用户是否选择了条件
            if (wheres.Count > 0)
            {
                string wh = string.Join(" and ", wheres.ToArray());
                sql.Append(" where " + wh);
            }

SqlHelper.ExecuteDataTable(sql.ToString(), listParameter.ToArray());
            #endregion

最新文章

  1. oracle 字符乱码问题解决方案
  2. ffmpeg mp3 to m3u8
  3. 【杂记】SQL篇
  4. Golang学习 - bufio 包
  5. BZOJ2298: [HAOI2011]problem a
  6. 得到某个进程所有线程ID和入口地址
  7. 仿微博——MJExtension之字典转模型
  8. Android 应用间的集成
  9. Qt编程学习网站
  10. MyEclipse性能调优初体验
  11. 链接分析算法之:主题敏感PageRank
  12. 用Open SSH生成公钥和私钥(Win)
  13. c语言——第0次作业
  14. C#备份及还原数据库的实现
  15. [trouble] error connecting to master &#39;repl@192.168.1.107:3306&#39; - retry-time: 60 retries: 86400
  16. Ubuntu 远程使用ssh 开启服务器终端的方法
  17. Selenium Webdriver元素定位的八种常用方式(转载)
  18. 有关Linux的.a、.so和.o文件(转)【原文章有些错误,自己已更改】
  19. Dalvik源码阅读笔记(二)
  20. luogu2038 [NOIp2014]无线网络发射器选址 (前缀和)

热门文章

  1. delphi xe10 网络文件传送
  2. StringUtils工具类常用api &lt;转&gt;
  3. 2018-2019-2-20175323 java实验三敏捷开发与XP实践
  4. 其它课程中的python---3、numpy总结(非常全)
  5. Instrumentation 实践详解
  6. Python匹马行天下之python之父
  7. 3-Ubuntu下python3安装pymysql模块(1)
  8. springboot中参数校验
  9. 在CentOS 7上安装常用的YUM源
  10. innodb 表