#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

C# SQL 多条件查询技巧的更多相关文章

  1. util-C# 复杂条件查询(sql 复杂条件查询)查询解决方案

    ylbtech-funcation-util:  C# 复杂条件查询(sql 复杂条件查询)查询解决方案 C# 复杂条件查询(sql 复杂条件查询)查询解决方案 1.A,Ylbtech.Model返回 ...

  2. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  3. SQLServer多条件查询技巧

    2019-10-15  13:31:04 在实际项目开发中,有很多页面都会出现多条件查询功能,类似于这种情况: 牵扯到数据就少不了数据库了.这么多条件的查询,如果要用常规的if else来写判断逻辑的 ...

  4. SQL多条件查询安全高效比较

    ALTER PROCEDURE _tmp @ID VARCHAR(50), @PN VARCHAR(50), @Type INT AS BEGIN /************************* ...

  5. Webform中linq to sql多条件查询(小练习)

    多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: <body> <form id="form1" ...

  6. C# SQL多条件查询拼接技巧

    本文转载:http://blog.csdn.net/limlimlim/article/details/8638080 #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) Stri ...

  7. SQL 多条件查询

    网上有不少人提出过类似的问题:“看到有人写了WHERE 1=1这样的SQL,到底是什么意思?”.其实使用这种用法的开发人员一般都是在使用动态组装的SQL.让我们想像如下的场景:用户要求提供一个灵活的查 ...

  8. SQL Server 一些查询技巧

    --1.[行列转换] --列转行 USE tempdb GO IF (OBJECT_ID('DEPT') IS NOT NULL) DROP TABLE DEPT CREATE TABLE DEPT( ...

  9. SQL 变量 条件查询 插入数据

    (本文只是总结网络上的教程) 在操作数据库时 SQL语句中难免会用到变量 比如 在條件值已知的情況下 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值 ...

随机推荐

  1. 暴力模拟——cf988E

    很简单的题,就是模拟一下 #include<bits/stdc++.h> using namespace std; #define ll long long ll n,a[],len; i ...

  2. LUOGU P4560 [IOI2014]Wall 砖墙 (线段树)

    传送门 解题思路 线段树打标记,刚开始想复杂了,维护了四个标记.后来才知道只需要维护一个最大值最小值即可,然后更新的时候分类讨论一下. 代码 #include<iostream> #inc ...

  3. APIO 2017 商旅 洛谷3778

    Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...

  4. NOIp2018集训test-9-6(am)

    Problem A. divisor 发现x为k可表达一定可以表示成这种形式,如k=3,x=(1/3+1/2+1/6)x. 于是可以搜索k(k<=7)个1/i加起来等于1的情况,如果一个数是这些 ...

  5. 开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误

    开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误, 调试后发现是由于写的SQL语句里列的数目和后面的值 ...

  6. 【POJ】1860 Currency Exchange

    真是气skr人..没把d[]换成double...de了一上午的bug// 记得用G++提交啊 题目链接:http://poj.org/problem?id=1860 题意:告诉你n个点,m条路.起始 ...

  7. arm-linux-copydump 的使用

    生成可以执行的 2 进制代码 [arm@localhost gcc]#arm­linux­copydump ­O binary hello hello.bin

  8. BCZM : 1.4

    书店促销活动,某套书一共有五卷.假设每一卷单独销售均需8欧元,多买则有折扣,具体折扣如下:    2 5%    3 10%    4 20%    5 25%    设计算法,计算出读者购买一批书的 ...

  9. 8.spark Core 进阶1

        (e.g. standalone manager, Mesos, YARN)   In "cluster" mode, the framework launches the ...

  10. Mysql: [Warning] Using a password on the command line interface can be insecure

    mysql: [Warning] Using a password on the command line interface can be insecure MySQL 5.6 警告信息 comma ...