mysql in查询 拼接_参数化拼接in查询条件,个人备份

该博客展示了如何在C#中动态拼接SQL的IN查询条件,使用参数化方式处理多个车牌号和司机工号,以实现数据查询。在查询过程中,还涉及到日期范围和分析角度的选择,最终通过SQLHelper获取数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///

/// 查询数据报表

///

/// 日期范围尾

/// 分析角度

/// 派车任务模型

///

public DataTable GetVehDisReport(string TrueOutTimeTo, string anslyse, VehDispTaskModel model)

{

string sql = string.Empty;//存储sql语句头

StringBuilder sqlWhere = new StringBuilder();//存储查询条件的sql

List listStr = new List();//用于动态存储参数最后转成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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值