Пример #1
0
 public int Update <T>(List <T> ts)
 {
     if (ts.Count == 0)
     {
         return(0);
     }
     using (var conn = GetConnection())
     {
         conn.Open();
         string sql = MyContainer.Get(typeof(T)).UpdateSqlStatement;
         using (var trans = conn.BeginTransaction())
         {
             try
             {
                 var rows = conn.Execute(sql, ts.ToArray(), trans, 30, CommandType.Text);
                 trans.Commit();
                 return(rows);
             }
             catch (DataException ex)
             {
                 trans.Rollback();
                 throw ex;
             }
         }
     }
 }
Пример #2
0
 public int Create <T>(List <T> ts)
 {
     if (ts.Count == 0)
     {
         return(0);
     }
     using (var conn = GetConnection())
     {
         conn.Open();
         var sql = MyContainer.Get(typeof(T)).InsertSql;
         using (var trans = conn.BeginTransaction())
         {
             try
             {
                 var rows = conn.Execute(sql, ts, trans, 30, CommandType.Text);
                 trans.Commit();
                 return(rows);
             }
             catch (DataException)
             {
                 trans.Rollback();
                 throw;
             }
         }
     }
 }
Пример #3
0
        public int Update <T>(object obj, MySearchUtil util)
        {
            using (var conn = GetConnection())
            {
                string where = util.GetConditionClaus();
                var param = util.GetParameters();

                var props  = obj.GetType().GetProperties();
                var entity = MyContainer.Get(typeof(T));

                StringBuilder sb    = new StringBuilder();
                List <string> _cols = new List <string>();

                List <string> propNames = new List <string>();
                foreach (var p in props)
                {
                    propNames.Add(p.Name);
                    param.Add(p.Name, p.GetValue(obj));
                    _cols.Add($"{p.Name}=@{p.Name}");
                }

                sb.Append($"UPDATE [{entity.TableName}] SET ");
                sb.Append(string.Join(',', _cols));
                sb.Append($" WHERE {where}");

                return(conn.Execute(sb.ToString(), obj));
            }
        }
Пример #4
0
 public int Create <T>(T t)
 {
     using (var conn = GetConnection())
     {
         string sql = MyContainer.Get(typeof(T)).InsertSqlStatement;
         return(conn.ExecuteScalar <int>(sql, t));
     }
 }
Пример #5
0
 public int Update <T>(T t)
 {
     using (var conn = GetConnection())
     {
         string sql = MyContainer.Get(typeof(T)).UpdateSqlStatement;
         return(conn.Execute(sql, t));
     }
 }
Пример #6
0
 /// <summary>
 /// 批量更新实体
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="ts">要更新的实体列表</param>
 /// <returns></returns>
 public int Update <T>(List <T> ts)
 {
     using (var conn = GetConnection())
     {
         string sql = MyContainer.Get(typeof(T)).UpdateSql;
         return(conn.Execute(sql, ts));
     }
 }
Пример #7
0
 public string GetCommonUpdateSqlStatement <T>()
 {
     try
     {
         return(MyContainer.Get(typeof(T)).UpdateSqlStatement);
     }
     catch
     {
         throw new Exception("指定类型未在MyContainer中注册");
     }
 }
Пример #8
0
 public string GetCommonUpdateSql <T>()
 {
     try
     {
         return(MyContainer.Get(typeof(T)).UpdateSql);
     }
     catch
     {
         return(string.Empty);
     }
 }
Пример #9
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t">要更新的实体</param>
        /// <returns></returns>
        public int Update <T>(T t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("要修改的实体为空");
            }

            using (var conn = GetConnection())
            {
                string sql = MyContainer.Get(typeof(T)).UpdateSql;
                return(conn.Execute(sql, t));
            }
        }
Пример #10
0
        /// <summary>
        /// 更新实体列表,指定不需要更新的列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ts">要更新的实体列表</param>
        /// <param name="exclude">不需要更新的列</param>
        /// <returns></returns>
        public int UpdateExclude <T>(List <T> ts, string[] exclude)
        {
            if (exclude.Length == 0)
            {
                throw new ArgumentException("必须指定要更新的列");
            }

            using (var conn = GetConnection())
            {
                var sql = MyContainer.Get(typeof(T)).GetUpdateSql(null, exclude, "default");
                return(conn.Execute(sql, ts));
            }
        }
Пример #11
0
 public string GetUpdateSqlStatement <T>(object obj, MySearchUtil util = null)
 {
     try
     {
         var entity = MyContainer.Get(typeof(T));
         var sql    = MyEntityUtil.BuildUpdateSqlByAnonymous(entity, obj, util == null ? "" : util.GetConditionClaus());
         return(sql);
     }
     catch
     {
         throw new Exception("指定类型未在MyContainer中注册");
     }
 }
Пример #12
0
 public string GetUpdateSqlStatement <T>(string[] columns, bool isExclude = false, MySearchUtil util = null)
 {
     try
     {
         var entity = MyContainer.Get(typeof(T));
         var sql    = MyEntityUtil.BuildUpdateSqlStatement(entity, columns, isExclude, util == null ? "" : util.GetConditionClaus());
         return(sql);
     }
     catch
     {
         throw new Exception("指定类型未在MyContainer中注册");
     }
 }
Пример #13
0
 public string GetInsertSqlStatement <T>(string[] columns, bool isExclude = false)
 {
     try
     {
         var entity = MyContainer.Get(typeof(T));
         var sql    = MyEntityUtil.BuildInsertSqlStatement(entity, columns, isExclude);
         return(sql);
     }
     catch
     {
         throw new Exception("指定类型未在MyContainer中注册");
     }
 }
Пример #14
0
 public string GetInsertSqlStatement <T>(object obj)
 {
     try
     {
         var entity = MyContainer.Get(typeof(T));
         var sql    = MyEntityUtil.BuildInsertSqlByAnonymous(entity, obj);
         return(sql);
     }
     catch
     {
         throw new Exception("指定类型未在MyContainer中注册");
     }
 }
Пример #15
0
        /// <summary>
        /// 更新实体的指定列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t">要更新的实体</param>
        /// <param name="include">要更新的列</param>
        /// <returns></returns>
        public int UpdateInclude <T>(T t, string[] include)
        {
            if (t == null)
            {
                throw new ArgumentNullException("要修改的实体为空");
            }

            if (include.Length == 0)
            {
                throw new ArgumentException("必须指定要更新的列");
            }

            using (var conn = GetConnection())
            {
                var sql = MyContainer.Get(typeof(T)).GetUpdateSql(include);
                return(conn.Execute(sql, t));
            }
        }
Пример #16
0
        /// <summary>
        /// 指定更新的列和值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="kvs">要更新的列和值</param>
        /// <param name="msu">要更新的数据条件</param>
        /// <returns></returns>
        public int Set <T>(KeyValuePairs kvs, MySearchUtil msu)
        {
            DynamicParameters param = new DynamicParameters();
            List <string>     props = new List <string>();

            foreach (var kv in kvs)
            {
                props.Add(kv.Key);
                param.Add(kv.Key, kv.Value);
            }

            var sql = MyContainer.Get(typeof(T)).GetUpdateSql(props.ToArray(), null, msu.ConditionClaus);

            using (var conn = GetConnection())
            {
                return(conn.Execute(sql, param));
            }
        }
Пример #17
0
 public string GetInsertSql <T>()
 {
     return(MyContainer.Get(typeof(T))?.InsertSql);
 }
Пример #18
0
        public string GetTableName(Type type)
        {
            var entityInfo = MyContainer.Get(type);

            return(entityInfo.TableName);
        }
Пример #19
0
 public string GetUpdateSql <T>()
 {
     return(MyContainer.Get(typeof(T))?.UpdateSql);
 }