示例#1
0
        /// <summary>
        ///     修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbs"></param>
        /// <param name="t">如果sql为null,则根据t的主键进行修改</param>
        /// <param name="updateProperties">要修改的属性集合</param>
        /// <param name="sql">按条件修改</param>
        /// <returns></returns>
        public static int Update <T>(this DbBase dbs, T t, IList <string> updateProperties, SqlQuery sql = null)
            where T : class
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var db = dbs.DbConnecttion;

            if (sql == null)
            {
                sql = SqlQuery <T> .Builder(dbs);
            }
            sql = sql.AppendParam(t)
                  .SetExcProperties <T>(updateProperties);

            SqlLog log = new SqlLog
            {
                CreateTime = DateTime.Now,
                OperateSql = sql.UpdateSql,
                Parameter  = sql.Param == null ? "" : sql.Param.ToString()
            };
            var result = db.Execute(sql.UpdateSql, sql.Param);

            stopwatch.Stop();
            log.EndDateTime = DateTime.Now;
            log.ElapsedTime = stopwatch.Elapsed.TotalSeconds;
            WriteSqlLog(log);
            return(result);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbs"></param>
        /// <param name="t">如果sql为null,则根据t的主键进行修改</param>
        /// <param name="sql">按条件修改</param>
        /// <returns></returns>
        public static bool Update <T>(this DbBase dbs, T t, SqlQuery sql = null) where T : class
        {
            var db = dbs.DbConnecttion;

            if (sql == null)
            {
                sql = SqlQuery <T> .Builder(dbs);
            }
            sql = sql.AppendParam <T>(t);
            var f = db.Execute(sql.UpdateSql, sql.Param);

            return(f > 0);
        }
示例#3
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="unitOfWork"></param>
        /// <param name="t"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static async Task <int> Update <T>(IUnitOfWork unitOfWork, T t, SqlQuery sql = null) where T : class
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var db = unitOfWork.DbConnection;

            if (sql == null)
            {
                sql = SqlQuery <T> .Builder(unitOfWork);
            }
            sql = sql.AppendParam(t);
            var result = await db.ExecuteAsync(sql.UpdateSql, sql.Param);

            stopwatch.Stop();
            return(result);
        }
        /// <summary>
        ///     修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbs"></param>
        /// <param name="t">如果sql为null,则根据t的主键进行修改</param>
        /// <param name="updateProperties">要修改的属性集合</param>
        /// <param name="sql">按条件修改</param>
        /// <returns></returns>
        public static async Task <int> Update <T>(this IUnitOfWork dbs, T t, IList <string> updateProperties, IDbTransaction transaction = null, SqlQuery sql = null)
            where T : class
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var db = dbs.DbConnection;

            if (sql == null)
            {
                sql = SqlQuery <T> .Builder(dbs);
            }
            sql = sql.AppendParam(t)
                  .SetExcProperties <T>(updateProperties);

            #region 之前的数据
            Type Ts     = t.GetType();
            var  prikey = Ts.GetProperty(DapperCacheCommon.GetPrimary(sql._ModelDes).Field).GetValue(t, null).ToString();
            var  obj    = dbs.DbConnection.Query(sql.QuerySqlById.Replace("@id", prikey.TrimEnd(',').SqlRemoveStr())).FirstOrDefault();
            #endregion

            //SqlLog log = new SqlLog
            //{
            //    CreateTime = DateTime.Now,
            //    OperateSql = sql.UpdateSql,
            //    Parameter = sql.Param == null ? "" : sql.Param.ToString()
            //};

            //DataLog datalog = new DataLog
            //{
            //    OperateType = OperateType.Edit.ToString(),
            //    OperateTable = sql._ModelDes.TableName,
            //    OperationBefore = JsonConvert.SerializeObject(obj),
            //    OperationAfterData = JsonConvert.SerializeObject(t),
            //};

            var result = await db.ExecuteAsync(sql.UpdateSql, sql.Param, transaction);

            stopwatch.Stop();

            //log.EndDateTime = DateTime.Now;
            //log.ElapsedTime = stopwatch.Elapsed.TotalSeconds;
            //WriteSqlLog(log);
            //WriteDataLog(datalog);
            return(result);
        }