/// <summary>
        /// 查询数据报表
        /// </summary>
        /// <param name="TrueOutTimeTo">日期范围尾</param>
        /// <param name="anslyse">分析角度</param>
        /// <param name="model">派车任务模型</param>
        /// <returns></returns>
        public DataTable GetVehDisReport(string TrueOutTimeTo, string anslyse, VehDispTaskModel model)
        {
            string sql = string.Empty;//存储sql语句头
            StringBuilder sqlWhere = new StringBuilder();//存储查询条件的sql

List<SqlParameter> listStr = new List<SqlParameter>();//用于动态存储参数最后转成sqlparameter[]即可

string[] arrLicensePlate = model.LicencePlate.Split(',');//存储勾选的所有车牌
            if (!string.IsNullOrEmpty(model.LicencePlate.Trim()))
            {
                //参数化拼接in('','','')查询语句
                string sLicensePlateCondition = " AND LicencePlate in (";
                for (int i = 0; i < arrLicensePlate.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arrLicensePlate[i]))
                    {
                        sLicensePlateCondition += "@Plate" + i+",";
                         listStr.Add( new SqlParameter("@Plate" + i, arrLicensePlate[i]));
                    }
                }
                sLicensePlateCondition = sLicensePlateCondition.TrimEnd(',');
                sLicensePlateCondition += ")";
                //加入查询条件
                sqlWhere.Append(sLicensePlateCondition);
            }

string[] arrDriverId = model.DriverId.Split(',');//存储所有勾选的司机工号
            if (!string.IsNullOrEmpty(model.DriverId))
            {
                //参数化拼接in('','','')查询语句
                string sDriverIdCondition = "AND DriverId in (";
                for (int i = 0; i < arrDriverId.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arrDriverId[i]))
                    {
                        sDriverIdCondition += "@DriverId" + i + ",";
                        listStr.Add(new SqlParameter("@DriverId" + i, arrDriverId[i]));
                    }
                }
                sDriverIdCondition = sDriverIdCondition.TrimEnd(',');
                sDriverIdCondition += ")";
                //加入查询条件
                sqlWhere.Append(sDriverIdCondition);

}
            if (!string.IsNullOrEmpty(model.VehDispTime.ToString()) && !string.IsNullOrEmpty(TrueOutTimeTo))
            {
                listStr.Add(new SqlParameter("@OutTimeFrom", model.VehDispTime));
                listStr.Add(new SqlParameter("@OutTimeTo",Convert.ToDateTime(TrueOutTimeTo)));
                sqlWhere.Append(" AND VehDispTime between cast(@OutTimeFrom as datetime) and cast(@OutTimeTo as datetime)");
            }
            if (anslyse == "1")//如果分析角度为1说明是司机,否则为公务车
            {
                sql = string.Format(@"select DriverId as '司机工号DriverId',DriverName as '司机姓名DriverName',Convert(varchar(10),VehDispTime,120)as '实际派车日期VehDispTime',
                                  round(sum(Datediff(hour,TrueoutTime,TrueBackTime)),0) as '派车时数Hours' from [dbo].[vwVehDispTask]
               where Status = 'Finished'" + "{0}" + "group by DriverId,DriverName,Convert(varchar(10),VehDispTime,120)", sqlWhere);
            }
            else
            {
                sql = string.Format(@"select  LicencePlate as '车牌LicencePlate',VehModel as '车型VehModel',Convert(varchar(10),VehDispTime,120)as '实际派车日期VehDispTime',
                                  round(sum(Datediff(hour,TrueoutTime,TrueBackTime)),0) as '派车时数Hours' from [dbo].[vwVehDispTask]
               where Status = 'Finished'" + "{0}" + "group by Convert(varchar(10),VehDispTime,120), LicencePlate,VehModel", sqlWhere);
            }
            SqlParameter[] param = listStr.ToArray();
            return DBHelper.GetDataSet(sql, param);
        }

参数化拼接in查询条件,个人备份的更多相关文章

  1. 论Top与ROW_NUMBER读取第一页的效率问题及拼接sql查询条件

    http://www.cnblogs.com/Leo_wl/p/4921799.html SELECT TOP * FROM users WHERE nID> And nID< ORDER ...

  2. mybatis 使用记录(二) 动态拼接查询条件

    2016-12-16 阅读项目代码时,在项目的xml文件中发现如下写法: SELECT student_user_id FROM tbr_student_class WHERE 1=1 <if ...

  3. Oracle,Mysql ,SQL Server 三大数据库带参数的模糊查询, 拼接查询条件问题

    最近项目开发一直在不断切换数据库,有时候一条sql 要同时考虑多种数据库中的兼容问题 , 先总结一条模糊查询拼接查询条件的问题,后续追加总结. 目前使用   mybatis: 1. Oracle 中使 ...

  4. .NetCore 使用 Linq 动态拼接Expression表达式条件来实现 对EF、EF Core 扩展查询排序操作

    相信在使用EF的时候对查询条件或者排序上的处理令人心烦,下面我们就来动态拼接表达式解决这一问题 当我们在查询中使用Where的时候可以看到如下参数 下面我们就来扩展 Expression<Fun ...

  5. 我的一个PLSQL函数 先查询再插入数据库的函数 动态SQL拼接查询条件、通用游标、记录定义(封装部分查询字段并赋值给游标)、insert select 序列、常量【我】

    先查询再插入数据库的函数 CREATE OR REPLACE FUNCTION F_REVENUE_SI(l_p_cd in Varchar2, l_c_cd in Varchar2, l_prod_ ...

  6. SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法

    本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...

  7. LINQ to SQL 运行时动态构建查询条件

    在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...

  8. Spring NamedParameterJdbcTemplate命名参数查询条件封装, NamedParameterJdbcTemplate查询封装

    Spring NamedParameterJdbcTemplate命名参数查询条件封装, NamedParameterJdbcTemplate查询封装 >>>>>> ...

  9. MyDAL - .Where() 之 .WhereSegment 根据条件 动态设置 Select查询条件 使用

    索引: 目录索引 一.API 列表 1.WhereSegment 属性,指示 根据条件 动态拼接 where 查询过滤条件 见如下示例. 二.API 单表-完整 方法 举例 // 上下文条件 变量 v ...

随机推荐

  1. day02.1-Linux虚拟操作系统的安装

    在windons环境中配置Linux虚拟操作系统,需要事先在Windons系统上下载并安装虚拟运行软件“VMware Workstations”,以下虚拟物理机的建立和虚拟Linux系统的配置都是在该 ...

  2. luogu2948 滑雪课

    题解里面全是dp的大神本蒟蒻瑟瑟发抖奉上一篇记忆化搜索... 其实嘛,记忆化搜索还是很安全透彻清真人品的,一般递推不好实现dp可以用记忆化搜索 然后本题先预处理一个mint[i]代表当前能力值为i,参 ...

  3. ribbon负载均衡

    ribbon的负载均衡策略有很多 IRule 这是所有负载均衡策略的父接口,里边的核心方法就是choose方法,用来选择一个服务实例. AbstractLoadBalancerRule Abstrac ...

  4. 在线作图工具 Flowchart Maker & Online Diagram Software & Visual Solution

    9款国内外垂直领域的在线作图工具:那些可以替代Visio的应用!-CSDN.NEThttps://www.csdn.net/article/2015-02-12/2823939 Documentsht ...

  5. winfom实现关闭后一直运行

    using PLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...

  6. JS执行顺序-函数声明提升、匿名函数、函数表达式

    大方向上: JS 是按照 代码块 进行 编译.执行 的. 学习至: 1.变量声明提升 2.新唐的博客 3.js中匿名函数的创建与调用方法分析 4.前端圣经 - <高程三> 5.深入理解变量 ...

  7. Til the Cows Come Home (dijkstra算法)

    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...

  8. 1065. A+B and C (64bit) (20)

    Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...

  9. java课内容2019.3.1

    java的相关了解博客:答疑 https://www.cnblogs.com/aishangJava/p/6862917.html 一.构造方法: 1.构造方法只能被编译器调用一次,并且是在创建对象时 ...

  10. vue.js vue-jsonp解决跨域问题

    安装jsonp npm install vue-jsonp --save main.js中引入 import VueJsonp from 'vue-jsonp' Vue.use(VueJsonp) 组 ...