decimal类型不能为空,自定义update更新null值的问题。

 if (!string.IsNullOrEmpty(yt_time_limit_1))
                {
                    entity["yt_time_limit_1"] = Convert.ToDecimal(yt_time_limit_1);
                }
                else
                {
                    entity["yt_time_limit_1"] = "null";
                }

cd.UpdateFaultComponent(entity);
 public bool UpdateFaultComponent(Entity entity)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yt_fault_component set ");
            strSql.Append("yt_name='" + entity["yt_name"] + "',");
            strSql.Append("yt_fault_assembly='" + ((EntityReference)entity["yt_fault_assembly"]).Id + "',");
                       strSql.Append("yt_distance_7=" + entity["yt_distance_7"] + "");
            strSql.Append("  where yt_code='" + entity["yt_code"] + "'");

            int rows = Convert.ToInt32(SqlHelper.SQLExecuteScalar(strSql.ToString(), sqlcon));
            return rows > 0 ? true : false;
        }

  这里如果不用crm标准//crmservice.Update(entity);标准写法null值正常,自定义更新方法拼接sql,更新的话decimal是不允许为空的,但是业务提出不能为空,得为null,这就出问题了,如果直接赋值null entiity传过去会是"",update语句就会报错,这个问题搞了我半天,
如果直接赋值0也没那么多事,但是必须null,解决办法就是这样,赋值“null”就可以了。

原文地址:https://www.cnblogs.com/zhaojingwei/p/4691535.html