/// <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); }
/// <summary> /// 全部查询 /// </summary> /// <typeparam name="T">查询的实体类型</typeparam> /// <returns></returns> public List <T> SelectAllGenericCacheDBDelegate <T>() where T : BaseModel { Type type = typeof(T); string sqlStr = GenericCacheSelectAllDB <T> .GetSelectAllSqlStr(); List <T> Ts = new List <T>(); Func <SqlCommand, List <T> > func = comm => { SqlDataReader reader = comm.ExecuteReader(); //SqlDataReader reader = ExexuteSql(sqlStr, SeletFunc); while (reader.Read()) { object o = Activator.CreateInstance(type); foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos())//缓存 { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); pro.SetValue(o, reader[dBNameAttribute.Name]); } else { pro.SetValue(o, reader[pro.Name]); } } T t = (T)o; Ts.Add(t); } return(Ts); }; return(this.ExexuteSql(sqlStr, func)); //return Ts; }
static GenericCacheSelectAllDB() { Type type = typeof(T); string tableName = type.Name; if (type.IsDefined(typeof(DBNameAttribute))) { DBNameAttribute dBNameAttribute = (DBNameAttribute)type.GetCustomAttribute(typeof(DBNameAttribute)); tableName = dBNameAttribute.Name; } List <string> proList = new List <string>(); foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); proList.Add(dBNameAttribute.Name); } else { proList.Add(pro.Name); } } string proesStr = string.Join(",", proList.Select(item => "[" + item + "]")); string sqlStr = " select " + proesStr + " from " + "[" + tableName + "]"; sqlSelectAllStr = sqlStr; }
static GetDBName() { Type type = typeof(T); string tableName = type.Name; if (type.IsDefined(typeof(DBNameAttribute))) { DBNameAttribute dBNameAttribute = (DBNameAttribute)type.GetCustomAttribute(typeof(DBNameAttribute)); tableName = dBNameAttribute.Name; } tableName_ = tableName; List <string> proList = new List <string>(); foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); proList.Add(dBNameAttribute.Name); } else { proList.Add(pro.Name); } } proNames_ = proList; }
/// <summary> /// 利用泛型缓存插入数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public bool InsertGenericCacheDB <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 + "]")); //string proesValStr = string.Join(",", type.GetProperties(). // Where(item => item.Name.ToLower() != "id").Select(item => "@" + item.Name)); //string sqlStr = "Insert into " + "[" + type.Name + "]" + "values" + "(" + proesValStr + ")"; string sqlStr = GenericCacheInsertDB <T> .GetInsertSql(); int rows = 0; using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB)) { SqlCommand comm = new SqlCommand(sqlStr, conn); var proes = GetTypeProGenericCache <T> .GetTypeProInfos().Where(item => item.Name.ToLower() != "id"); foreach (var pro in proes) { 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> /// 按照id查询一个实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="id">实体的id值</param> /// <returns></returns> public T FindGenericCacheDB <T>(int id) where T : BaseModel { Type type = typeof(T); //string proesStr = string.Join(",", type.GetProperties().Select(item => "[" + item.Name + "]")); //string strSql = "Select" + proesStr + "from [" + type.Name + "]where [id]=@Id" ; string strSql = GenericCacheFindDB <T> .GetFindSqlStr(); object o = Activator.CreateInstance(type); using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB)) { SqlCommand comm = new SqlCommand(strSql, conn); SqlParameter para = new SqlParameter("@Id", id); comm.Parameters.Add(para); conn.Open(); try { SqlDataReader reader = comm.ExecuteReader(); if (reader.Read()) { foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); pro.SetValue(o, reader[dBNameAttribute.Name]); } else { pro.SetValue(o, reader[pro.Name]); } } return((T)o); } else { return(null); } } catch (Exception e) { conn.Close(); throw e; } } //return default(T); }
/// <summary> /// 全部查询 /// </summary> /// <typeparam name="T">查询的实体类型</typeparam> /// <returns></returns> public List <T> SelectAllGenericCacheDB <T>() where T : BaseModel { Type type = typeof(T); //string proesStr = string.Join(",", type.GetProperties().Select(item => "[" + item.Name + "]")); //string sqlStr = " select " + proesStr + " from " + "[" + type.Name + "]"; string sqlStr = GenericCacheSelectAllDB <T> .GetSelectAllSqlStr(); List <T> Ts = new List <T>(); using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB)) { SqlCommand comm = new SqlCommand(sqlStr, conn); conn.Open(); try { SqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { object o = Activator.CreateInstance(type); foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); pro.SetValue(o, reader[dBNameAttribute.Name]); } else { pro.SetValue(o, reader[pro.Name]); } } T t = (T)o; Ts.Add(t); } } catch (Exception e) { conn.Close(); throw e; } } return(Ts); }
/// <summary> /// 利用泛型缓存插入数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public bool InsertGenericCacheDBDelegate <T>(T t) where T : BaseModel { Type type = typeof(T); string sqlStr = GenericCacheInsertDB <T> .GetInsertSql(); int rows = 0; Func <SqlCommand, int> func = comm => { var proes = GetTypeProGenericCache <T> .GetTypeProInfos().Where(item => item.Name.ToLower() != "id"); foreach (var pro in proes) { 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); } }
/// <summary> /// 按照id查询一个实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="id">实体的id值</param> /// <returns></returns> public T FindGenericCacheDBDelegate <T>(int id) where T : BaseModel { Type type = typeof(T); //string proesStr = string.Join(",", type.GetProperties().Select(item => "[" + item.Name + "]")); //string strSql = "Select" + proesStr + "from [" + type.Name + "]where [id]=@Id" ; string strSql = GenericCacheFindDB <T> .GetFindSqlStr(); object o = Activator.CreateInstance(type); SqlParameter para = new SqlParameter("@Id", id); Func <SqlCommand, T> func = comm => { comm.Parameters.Add(para); SqlDataReader reader = comm.ExecuteReader(); //SqlDataReader reader = ExexuteSql(strSql, SeletFunc); if (reader.Read()) { foreach (var pro in GetTypeProGenericCache <T> .GetTypeProInfos()) { if (pro.IsDefined(typeof(DBNameAttribute), true)) { DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute)); pro.SetValue(o, reader[dBNameAttribute.Name]); } else { pro.SetValue(o, reader[pro.Name]); } } return((T)o); } else { return(default(T)); } //return default(T); }; return(this.ExexuteSql(strSql, func)); }