本文转载:http://blog.csdn.net/limlimlim/article/details/8638080

   #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条件的类 

http://dotnet.chinaitlab.com/CSharp/746379_3.html

http://blog.csdn.net/dj1232090/article/details/2476224

http://blog.sina.com.cn/s/blog_3d7bed6501000c85.html

列表查询组件代码, 简化拼接条件SQL语句的麻烦

http://www.cnblogs.com/wuhuacong/archive/2007/11/19/964100.html

多条件搜索分页的实现

http://www.cnblogs.com/dedeyi/archive/2013/03/13/2957790.html

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

  1. sql 多条件查询 拼接字符串 改成 普通查询格式

    set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC [dbo].[usp_SRV_CheckServiceDemandOrder] ) = ...

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

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

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

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

  4. C# SQL 多条件查询技巧

    #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneN ...

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

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

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

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

  7. SQL 多条件查询

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

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

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

  9. sql---如何把sql查询出来的结果当做另一个sql的条件查询,1、语句2、with as

    '; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多 ...

随机推荐

  1. bootstrap轮播组件,大屏幕图片居中效果

    在慕课网学习bootstrap轮播组件的时候,了解到轮播的图片都放在了类名为item下的img中 视频中老师对图片自适应采用给图片img设置width=100%完成,然而这样自适应处理图片在不同屏幕中 ...

  2. TCPIP通信

    最近在开发TCPIP通信,封装了3个类,望各位大神指点指点. using System; using System.Collections.Generic; using System.Text; us ...

  3. Asp.net服务器控件在IE10下的不兼容问题

    Asp.net服务器控件在IE10下的不兼容问题 时间:2013-05-16 09:07点击: 89 次 [大 中 小] 相信很多使用IE10的童鞋们已经发现了这个问题,以下是本人在IE10标准模式下 ...

  4. php中include文件夹分析

    include是包含很多php文件的一种汇总:一般放在文件夹最外层. <?php header("content-type:text/html;charset=utf-8") ...

  5. AS3.0的int uint Number的使用原则

    int uint Number的使用原则: 1.能用整数值时优先使用:int uint 2.整数值有正负时使用:int 3.只处理正整数时使用:uint 4.处理好和颜色相关的值时使用:uint 5. ...

  6. C# Winform开发框架模块图(平台核心模块+示例模块)

    企业版V4.0 - 模块图   企业版V4.0 - 项目解决方案   Client/Server构架,有两个解决方案:     客户端解决方案说明:     服务端解决方案说明: C/S系统开发框架- ...

  7. 2013Java最新面试题

    更新时间:2015-04-07         来源:网络         投诉删除 [看准网(Kanzhun.com)]Java面试题频道小编搜集的范文“2013Java最新面试题”,供大家阅读参考 ...

  8. Flex时间操作

    小弟是Flex新手,最近一段时间领导要求使用Flex开发B/S的一些项目,需要用到时间上的一些操作.上网查询一番好多人都说不好操作,有的甚至非常麻烦.基于此,小弟整理了一些关于Flex时间操作的经验, ...

  9. vc2010配置opencv2.4.4库(图文 转)

        VC 2010下安装OpenCV2.4.4   说明: 安装平台:32位XP,VS2010: OpenCV 2.4.4不支持VC 6.0: 网上有很多用CMake编译OpenCV的安装教程,这 ...

  10. 直接将视频文件原码流转换成YUV,输出到屏幕显示

    #include "stdafx.h" #define inline _inline#ifndef INT64_C#define INT64_C(c) (c ## LL)#defi ...