/// <summary> /// 解析表达式目录树 完成批量修改的功能 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="exp"></param> /// <param name="updateAction"></param> public void UpdateSqlStrExpr <T>(T t, Expression <Func <T, bool> > exp, Action <T> updateAction) where T : BaseModel { string sqlStrRequ = vistor.GetSqlStrRequire <T>(exp); updateAction = x => { Type type = typeof(T); string proesStr = string.Join(",", GetDBName <T> .GetProNames(). Where(item => item.ToLower() != "id").Select(item => "[" + item + "]=" + "@" + item)); string sqlStr = "update [" + GetDBName <T> .GetTableName() + "] set " + proesStr + " where " + sqlStrRequ; 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()); }; this.ExexuteSql(sqlStr, func); }; updateAction.Invoke(t); }
static GenericCacheUpdateDB() { Type type = typeof(T); string proesStr = string.Join(",", GetDBName <T> .GetProNames(). Where(item => item.ToLower() != "id").Select(item => "[" + item + "]=" + "@" + item)); string sqlStr = "update [" + GetDBName <T> .GetTableName() + "] set " + proesStr + " where Id=@Id"; sqlUpdateStr = sqlStr; }
/// <summary> /// 静态构造函数执行一次 /// </summary> static GenericCacheInsertDB() { Type type = typeof(T); string proesStr = string.Join(",", GetDBName <T> .GetProNames(). Where(item => item.ToLower() != "id").Select(item => "[" + item + "]")); string proesValStr = string.Join(",", GetDBName <T> .GetProNames(). Where(item => item.ToLower() != "id").Select(item => "@" + item)); string sqlStr = "Insert into " + "[" + GetDBName <T> .GetTableName() + "](" + proesStr + ")values" + "(" + proesValStr + ")"; _sqlString = sqlStr; }
static GenericCacheFindDB() { //Type type = typeof(T); try { string proesStr = string.Join(",", GetDBName <T> .GetProNames().Select(item => "[" + item + "]")); string strSql = "Select" + proesStr + "from [" + GetDBName <T> .GetTableName() + "]where [id]=@Id"; sqlFindStr = strSql; } catch (Exception e) { LogManager.WriteLog("GenericCacheFindDB", "GenericCacheFindDB(static)", e.Message); //throw e; } }