/// <summary> /// 生成更新语句 /// </summary> /// <param name="model"></param> /// <returns></returns> public virtual string BuildUpdateSQL(Model.Model model) { List <string> fileds = new List <string>(); List <string> wheres = new List <string>(); var propertys = model.GetType().GetProperties(); var primarys = model.getPrimaryKeys(); var identity = model.getIdentify(); foreach (var p in propertys) { var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false); if (notmaps != null && notmaps.Length > 0) { continue; } if (primarys.Any(m => m.Name == p.Name)) { wheres.Add(string.Format("[{0}]=@{0}", p.Name)); } if (!Magic.Mvc.Equals.IsNull(identity) && identity.Name == p.Name) { continue; } fileds.Add(string.Format("[{0}]=@{0}", p.Name)); } return(string.Format(" UPDATE [{0}] SET {1} WHERE {2}", model.GetType().Name, string.Join(",", fileds.ToArray()), string.Join(" and ", wheres.ToArray()))); }
/// <summary> /// 生成删除语句 /// </summary> /// <param name="model"></param> /// <returns></returns> public virtual string BuildDeleteSQL(Model.Model model) { List <string> wheres = new List <string>(); var primarys = model.getPrimaryKeys(); foreach (var p in primarys) { var notmaps = p.GetCustomAttributes(typeof(Model.NotMapingAttribute), false); if (notmaps != null && notmaps.Length > 0) { continue; } wheres.Add(string.Format("[{0}]=@{0}", p.Name)); } return(string.Format(" DELETE FROM [{0}] WHERE {1}", model.GetType().Name, string.Join(" and ", wheres.ToArray()))); }