Пример #1
0
        /// <summary>
        /// 修改局部字段
        /// </summary>
        /// <param name="modelWhere">条件</param>
        /// <param name="modelValue">字段</param>
        /// <returns></returns>
        public bool UpdateArticleNewList(ArticleNewList modelWhere, ArticleNewList modelValue)
        {
            if (modelWhere != null && modelValue != null)
            {
                string strSql = @"update [Marketing].[dbo].[tbl_ArticleNewList]  WITH(rowlock) set {0} WHERE 1=1 {1}";

                StringBuilder strSqlWhere = new StringBuilder();
                Dictionary <string, object> dicParamsWhere = new Dictionary <string, object>();
                Dictionary <string, object> dicParamsValue = new Dictionary <string, object>();

                #region 更新字段
                if (modelValue.ArticleId > 0)
                {
                    dicParamsValue.Add("ArticleId", modelValue.ArticleId);
                }
                if (!string.IsNullOrEmpty(modelValue.ArticleUrl))
                {
                    dicParamsValue.Add("ArticleUrl", modelValue.ArticleUrl);
                }
                if (!string.IsNullOrEmpty(modelValue.ProductId))
                {
                    dicParamsValue.Add("ProductId", modelValue.ProductId);
                }
                if (modelValue.Type > 0)
                {
                    dicParamsValue.Add("Type", modelValue.Type);
                }
                if (DateTime.Compare(modelValue.CreateTime, DateTime.MinValue) > 0)
                {
                    dicParamsValue.Add("CreateTime", modelValue.CreateTime);
                }
                if (!string.IsNullOrEmpty(modelValue.Field_1))
                {
                    dicParamsValue.Add("Field_1", modelValue.Field_1);
                }
                if (!string.IsNullOrEmpty(modelValue.Field_2))
                {
                    dicParamsValue.Add("Field_2", modelValue.Field_2);
                }
                if (!string.IsNullOrEmpty(modelValue.Field_3))
                {
                    dicParamsValue.Add("Field_3", modelValue.Field_3);
                }
                if (!string.IsNullOrEmpty(modelValue.Field_4))
                {
                    dicParamsValue.Add("Field_4", modelValue.Field_4);
                }
                #endregion

                #region 更新条件
                if (modelWhere.Id > 0)
                {
                    dicParamsWhere.Add("Id", modelWhere.Id);
                }
                if (modelWhere.ArticleId > 0)
                {
                    dicParamsWhere.Add("ArticleId", modelWhere.ArticleId);
                }
                if (!string.IsNullOrEmpty(modelWhere.ArticleUrl))
                {
                    dicParamsWhere.Add("ArticleUrl", modelWhere.ArticleUrl);
                }
                if (!string.IsNullOrEmpty(modelValue.ProductId))
                {
                    dicParamsWhere.Add("ProductId", modelWhere.ProductId);
                }
                if (modelWhere.Type > 0)
                {
                    dicParamsWhere.Add("Type", modelWhere.Type);
                }
                if (DateTime.Compare(modelWhere.CreateTime, DateTime.MinValue) > 0)
                {
                    dicParamsWhere.Add("CreateTime", modelWhere.CreateTime);
                }
                if (!string.IsNullOrEmpty(modelWhere.Field_1))
                {
                    dicParamsWhere.Add("Field_1", modelWhere.Field_1);
                }
                if (!string.IsNullOrEmpty(modelWhere.Field_2))
                {
                    dicParamsWhere.Add("Field_2", modelWhere.Field_2);
                }
                if (!string.IsNullOrEmpty(modelWhere.Field_3))
                {
                    dicParamsWhere.Add("Field_3", modelWhere.Field_3);
                }
                if (!string.IsNullOrEmpty(modelWhere.Field_4))
                {
                    dicParamsWhere.Add("Field_4", modelWhere.Field_4);
                }
                #endregion

                StringBuilder  strSqlField = new StringBuilder();                                           //字段
                SqlParameter[] sqlParams   = new SqlParameter[dicParamsWhere.Count + dicParamsValue.Count]; //参数值

                for (int i = 0; i < dicParamsWhere.Count; i++)
                {
                    var dicKey   = dicParamsWhere.ElementAt(i).Key;
                    var dicValue = dicParamsWhere.ElementAt(i).Value;
                    sqlParams[i] = new SqlParameter("@Where" + dicKey, dicValue);
                    strSqlWhere.AppendFormat(" AND {0} = @Where{0} ", dicKey);
                }

                for (int i = 0; i < dicParamsValue.Count; i++)
                {
                    var dicKey   = dicParamsValue.ElementAt(i).Key;
                    var dicValue = dicParamsValue.ElementAt(i).Value;
                    sqlParams[dicParamsWhere.Count + i] = new SqlParameter("@Value" + dicKey, dicValue);
                    strSqlField.AppendFormat("{0}=@Value{0},", dicKey);
                }

                strSql = string.Format(strSql, strSqlField.ToString().TrimEnd(','), strSqlWhere.ToString());

                if (strSql.ToLower().IndexOf("and") > 0)
                {
                    Func <SqlConnection, bool> action = (connection) => DalArticle.UpdateArticleNewList(connection, strSql.ToString(), sqlParams);
                    return(dbManager.Execute(action));
                }
            }
            return(false);
        }
Пример #2
0
 /// <summary>
 /// 修改所有字段
 /// </summary>
 /// <param name="articleModel"></param>
 /// <returns></returns>
 public bool UpdateArticleNewList(ArticleNewList articleModel)
 {
     return(dbManager.Execute(conn => DalArticle.UpdateArticleNewList(conn, articleModel)));
 }