/// <summary> /// 更新数据库中的数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public bool UpdateGenericCacheDB <T>(T t) where T : BaseModel { Type type = typeof(T); //string proesStr = string.Join(",", type.GetProperties(). // Where(item => item.Name.ToLower() != "id").Select(item => "[" + item.Name + "]=" + "@" + item.Name)); //string sqlStr = "update [" + type.Name + "] set " + proesStr + " where id=@Id"; string sqlStr = GenericCacheUpdateDB <T> .GetUpdStr(); int rows = 0; using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB)) { SqlCommand comm = new SqlCommand(sqlStr, conn); foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { SqlParameter para = new SqlParameter(); if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); para = new SqlParameter("@" + dBNameAttribute.Name, pro.GetValue(t)); } else { para = new SqlParameter("@" + pro.Name, pro.GetValue(t)); } comm.Parameters.Add(para); } conn.Open(); try { rows = comm.ExecuteNonQuery(); } catch (Exception e) { conn.Close(); throw e; } } if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 更新数据库中的数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public bool UpdateGenericCacheDBDelegate <T>(T t) where T : BaseModel { Type type = typeof(T); string sqlStr = GenericCacheUpdateDB <T> .GetUpdStr(); int rows = 0; Func <SqlCommand, int> func = comm => { foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { SqlParameter para = new SqlParameter(); if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); para = new SqlParameter("@" + dBNameAttribute.Name, pro.GetValue(t)); } else { para = new SqlParameter("@" + pro.Name, pro.GetValue(t)); } comm.Parameters.Add(para); } return(comm.ExecuteNonQuery()); }; rows = this.ExexuteSql(sqlStr, func); if (rows > 0) { return(true); } else { return(false); } }