/// <summary> /// 备份数据库 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool BackDb(Entity.SysDbBack entity, out string msg) { msg = "ok"; if (entity == null) { msg = "实体为空"; return(false); } var site_config = Tools.ConfigHelper.GetSiteModel(); if (!System.IO.Directory.Exists(site_config.DbBackPath)) { try { System.IO.Directory.CreateDirectory(site_config.DbBackPath); } catch (Exception ex) { msg = "创建数据库备份目录失败:" + ex.Message; } } string is_diff = entity.BackType == Entity.SysDbBackType.diff ? " WITH DIFFERENTIAL " : ""; string back_path = site_config.DbBackPath + entity.DbName + "_" + entity.BackName + ".bak"; string backSql = string.Format("BACKUP DATABASE {0} to DISK = '{1}' {2};", entity.DbName, back_path, is_diff); BLL.BaseBLL <Entity.SysDbBack> bll = new BaseBLL <Entity.SysDbBack>(); entity.AddTime = DateTime.Now; entity.FilePath = back_path; bll.Add(entity); using (var db = new DataCore.EFDBContext()) { db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, backSql); } return(true); }
/// <summary> /// 修改分类数据 /// </summary> /// <returns></returns> public static bool Modify(Entity.CusCategory entity) { if (entity == null) { return(false); } var db = new DataCore.EFDBContext(); Entity.CusCategory p_entity = null; if (entity.PID != null) { p_entity = db.CusCategorys.Find(entity.PID); if (p_entity == null) { entity.PID = null; entity.Depth = 1; } else { entity.Depth = p_entity.Depth + 1; } } db.Entry <Entity.CusCategory>(entity).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); db.Dispose(); return(true); }
/// <summary> /// 删除备份的数据库 /// </summary> /// <param name="ids"></param> /// <param name="msg"></param> /// <returns></returns> public bool Del(string ids, out string msg, out string del_file) { msg = "ok"; del_file = ""; using (var db = new DataCore.EFDBContext()) { string path_sql = string.Format("select FilePath from SysDbBack where id in({0})", ids); var db_paths = db.Database.SqlQuery <string>(path_sql).ToList(); if (db_paths.Count == 0) { msg = "没有找到相关数据"; return(false); } del_file = string.Join(",", db_paths.ToArray()); string del_path = string.Format("delete SysDbBack where ID in({0})", ids); var total = db.Database.ExecuteSqlCommand(del_path); if (total <= 0) { msg = "未能删除成功"; return(false); } foreach (var path in db_paths) { Tools.IOHelper.DeleteIOFile(path); } return(true); } }
/// <summary> /// 添加分类数据 /// </summary> /// <returns></returns> public static int Add(Entity.CusCategory entity) { if (entity == null) { return(-1); } var db = new DataCore.EFDBContext(); Entity.CusCategory p_entity = null; if (entity.PID != null) { p_entity = db.CusCategorys.Find(entity.PID); if (p_entity == null) { entity.PID = null; entity.Depth = 1; } else { entity.Depth = p_entity.Depth + 1; } } db.CusCategorys.Add(entity); db.SaveChanges(); db.Dispose(); return(entity.ID); }
public void TreeTest() { var db = new DataCore.EFDBContext(); List <Universal.Web.Areas.Admin.Models.ViewModelTree> list = new List <Web.Areas.Admin.Models.ViewModelTree>(); var query = db.SysRoutes.GroupBy(p => p.Tag).ToList(); for (int i = 0; i < query.Count; i++) { int pid = TypeHelper.ObjectToInt(WebHelper.GenerateRandomIntNumber(4)); Universal.Web.Areas.Admin.Models.ViewModelTree model = new Web.Areas.Admin.Models.ViewModelTree(); model.id = pid; model.name = query[i].Key; model.open = i < 4 ? true : false; model.pId = 0; list.Add(model); foreach (var item in query[i].ToList()) { Universal.Web.Areas.Admin.Models.ViewModelTree model2 = new Web.Areas.Admin.Models.ViewModelTree(); model2.id = item.ID; model2.name = item.Desc; model2.open = false; model2.pId = pid; list.Add(model2); } } string json = Newtonsoft.Json.JsonConvert.SerializeObject(list); db.Dispose(); Assert.AreEqual(1, 1); }
/// <summary> /// 获取系统已存在的数据库 /// </summary> /// <returns></returns> public List <Entity.SysDbInfo> GetSysDbList() { using (var db = new DataCore.EFDBContext()) { string strSql = "select name,filename,crdate from sysdatabases WHERE name not in('master','tempdb','model','msdb')"; return(db.Database.SqlQuery <Entity.SysDbInfo>(strSql).ToList()); } }
/// <summary> /// 获取某个分类下所有的子类 /// </summary> /// <param name="id"></param> /// <returns></returns> public static string GetChildIDStr(int id) { using (var db = new DataCore.EFDBContext()) { string Sql = "select dbo.fn_GetChildCusCategoryStr(" + id.ToString() + ") as idstr"; return(db.Database.SqlQuery <string>(Sql).ToList()[0]); } }
/// <summary> /// 根据ID获取子或父数据 /// </summary> /// <param name="up">查找父级,否则为查找子级</param> /// <param name="id">当前分类ID</param> /// <returns></returns> public static List <Entity.CusCategory> GetList(bool up, int id) { List <Entity.CusCategory> list = new List <Entity.CusCategory>(); var db = new DataCore.EFDBContext(); SqlParameter[] param = { new SqlParameter("@Id", id) }; string proc_name = "dbo.sp_GetParentCusCategory @Id"; if (!up) { proc_name = "dbo.sp_GetChildCusCategory @Id"; } list = db.CusCategorys.SqlQuery(proc_name, param).ToList(); db.Dispose(); return(list); }
/// <summary> /// 还原数据库 /// </summary> /// <param name="entity"></param> /// <param name="msg"></param> /// <returns></returns> public bool RestoreDb(Entity.SysDbBack entity, out string msg) { msg = "ok"; if (entity == null) { msg = "实体为空"; return(false); } try { using (var db = new DataCore.EFDBContext()) { string restore_sql = string.Format("use master; ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE;RESTORE DATABASE {1} FROM DISK='{2}'WITH replace", entity.DbName, entity.DbName, entity.FilePath); db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, restore_sql); } } catch (Exception ex) { msg = "还原数据库出错:" + ex.Message; return(false); } return(true); }
/// <summary> /// 设置消息已读 /// </summary> /// <param name="id">-1时全部,否则单挑</param> /// <returns></returns> public static bool SetMsgRead(int id, out string msg) { msg = "ok"; using (var db = new DataCore.EFDBContext()) { if (id == -1) { db.Database.ExecuteSqlCommand("Update SysMessage set IsRead = 1 where IsRead = 0"); } else { var entity = db.SysMessages.Find(id); if (entity != null) { if (!entity.IsRead) { entity.IsRead = true; db.SaveChanges(); } } } } return(true); }