Dapper sql in

应用场景:

使用 sql的 Case When Then 批量更新某张表,底层数据库用到了Dapper

代码示例:

复制代码
public int UpdateClientReceivedResult(Dictionary<string, string> dict)
        {
            var dynamicSqlParam = new DynamicParameters();
            var sbSet = new StringBuilder(256);
            sbSet.Append(" (CASE SmsFlowID ");
            int idx = 0;
            foreach (var kv in dict)
            {
                sbSet.Append(string.Format(" WHEN @SmsFlowID{0} THEN @ClientReceivedResult{0} ", idx));
                dynamicSqlParam.Add(string.Format("SmsFlowID{0}", idx), kv.Key, null, null, null);
                dynamicSqlParam.Add(string.Format("ClientReceivedResult{0}", idx), kv.Value, null, null, null);
                idx += 1;
            }
            sbSet.Append(" ELSE ClientReceivedResult END) ");

            var sbWhere = new StringBuilder(256);
            sbWhere.Append(" AND SmsFlowID in @ids ");
            dynamicSqlParam.Add("ids", dict.Keys.ToArray(), null, null, null);

            string sql = string.Format("UPDATE [SmsSendResult] SET  [ClientReceivedResult]={0} WHERE 1=1 {1}", sbSet,
                sbWhere);
            int affectNum = -1;
            using (var conn = new SqlConnection(SmsDBConnString))
            {
                affectNum = conn.Execute(sql, dynamicSqlParam);
            }
            return affectNum;
        }
复制代码

最终生成的sql:

UPDATE [SmsSendResult] SET  [ClientReceivedResult]= (CASE SmsFlowID  WHEN @SmsFlowID0 THEN @ClientReceivedResult0  WHEN @SmsFlowID1 THEN @ClientReceivedResult1  WHEN @SmsFlowID2 THEN @ClientReceivedResult2  WHEN @SmsFlowID3 THEN @ClientReceivedResult3  WHEN @SmsFlowID4 THEN @ClientReceivedResult4  WHEN @SmsFlowID5 THEN @ClientReceivedResult5  WHEN @SmsFlowID6 THEN @ClientReceivedResult6  WHEN @SmsFlowID7 THEN @ClientReceivedResult7  ELSE ClientReceivedResult END)  WHERE 1=1  AND SmsFlowID in @SmsFlowID

代码说明:
这个方法的作用是,根据传进来的Dict<SmsFlowID,ClientReceivedResult>批量更新SmsSendResult对应的值,为了限定更新的条数,需要用到where in操作,in后面的参数名必须要注意一下,尽量不和别的参数名字有重复,我之前sbWhere是这么写的:

var sbWhere = new StringBuilder(256);
sbWhere.Append(" AND SmsFlowID in @SmsFlowID ");
dynamicSqlParam.Add("SmsFlowID", dict.Keys.ToArray(), null, null, null);

结果运行报异常:说什么已存在相同参数名SmsFlowID1。。。找了半天,原来是in的参数名字的问题!Dapper对于 where col in @arg 会将动态参数的数组类型的参数自动转化为 where col in (@arg1,@arg2....),这样就重名了,所以找了半天原因,原来是这么个回事,修改后程序运行正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值