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; } } } }
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; } } } }
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)); } }
public int Create <T>(T t) { using (var conn = GetConnection()) { string sql = MyContainer.Get(typeof(T)).InsertSqlStatement; return(conn.ExecuteScalar <int>(sql, t)); } }
public int Update <T>(T t) { using (var conn = GetConnection()) { string sql = MyContainer.Get(typeof(T)).UpdateSqlStatement; return(conn.Execute(sql, t)); } }
/// <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)); } }
public string GetCommonUpdateSqlStatement <T>() { try { return(MyContainer.Get(typeof(T)).UpdateSqlStatement); } catch { throw new Exception("指定类型未在MyContainer中注册"); } }
public string GetCommonUpdateSql <T>() { try { return(MyContainer.Get(typeof(T)).UpdateSql); } catch { return(string.Empty); } }
/// <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)); } }
/// <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)); } }
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中注册"); } }
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中注册"); } }
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中注册"); } }
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中注册"); } }
/// <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)); } }
/// <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)); } }
public string GetInsertSql <T>() { return(MyContainer.Get(typeof(T))?.InsertSql); }
public string GetTableName(Type type) { var entityInfo = MyContainer.Get(type); return(entityInfo.TableName); }
public string GetUpdateSql <T>() { return(MyContainer.Get(typeof(T))?.UpdateSql); }