示例#1
0
        public async Task <int> UpdateIfNotExitsAsync <T>(T entity, Expression <Func <T, bool> > where)
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var resolver   = new EditConditionResolver <T>(entityInfo);
            var result     = resolver.Resolve(where.Body);
            var condition  = result.Condition;
            var parameters = result.Parameters;

            parameters.Add(entity);

            condition = string.IsNullOrWhiteSpace(condition) ? "1=1" : condition;

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            sql += $" AND NOT EXISTS (SELECT 1 FROM [{entityInfo.TableName}] WHERE {condition})";

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                return(await command.ExecuteNonQueryAsync());
            }
        }
示例#2
0
        public async Task <int> UpdateAsync <T>(T entity) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            var parameters = new MyDbParameters();

            parameters.Add(entity);

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(parameters.Parameters);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                return(await command.ExecuteNonQueryAsync());
            }
        }
示例#3
0
        /// <summary>
        /// 更新多个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityList">要更新的实体列表</param>
        /// <returns>受影响的记录数</returns>
        public int Update <T>(List <T> entityList) where T : class, IEntity, new()
        {
            var entityInfo = MyEntityContainer.Get(typeof(T));

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Update(entityInfo, "");

            var count = 0;

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (var entity in entityList)
                        {
                            using (var command = new SqlCommand(sql, conn, trans))
                            {
                                var param = new MyDbParameters();
                                param.Add(entity);
                                command.Parameters.AddRange(param.Parameters);
                                count += command.ExecuteNonQuery();
                            }
                        }
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.Rollback();
                        count = 0;
                    }
                }
            }

            return(count);
        }