/// <summary> /// 渲染模板 /// </summary> public static void Render() { try { cms_channel site = new cms_channel(); //string file = Environment.CurrentDirectory + "home.html"; // var template = File.ReadAllText(file); // 根据路径读取模板内容 //var templateStr = Template.FileSystem.ReadTemplateFile(new Context(CultureInfo.InvariantCulture),file); //// 解析模板,这里可以缓存Parse出来的对象,但是为了简单这里就略去了 //var template = Template.Parse(templateStr, Hash.FromAnonymousObject(new { name = "World" }); //var result = template.Render(); //System.Reflection.FieldInfo[] fieldsInfo = site.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] fieldsInfo = site.GetType().GetProperties(); List <string> fields = new List <string>(); foreach (PropertyInfo fieldInfo in fieldsInfo) { fields.Add(fieldInfo.Name); } //Template.RegisterSafeType(typeof(cms_channel), fields.ToArray()); Template.RegisterSafeType(typeof(cms_channel), x => x); var template = Template.Parse("Hello, {{ site.site_description}}!,f**k:{{site.site_keyword}}"); //var result = template.Render(Hash.FromAnonymousObject(new{ site1=site })); var result = template.Render(Hash.FromAnonymousObject(new { site = site })); XTrace.WriteLine(result); } catch (Exception ex) { XTrace.WriteLine(ex.Message); } }
/// <summary> ///新增数据 /// </summary> /// <param name="dto"></param> public cms_channel Insert(cms_channel dto) { var id = Sqldb.Insert(dto).ExecuteIdentity(); dto.id = (int)id; return(dto); }
public IActionResult Save(cms_channel dto, int sub_channel) { dto.channel_href = dto.channel_href ?? ""; dto.channel_image = dto.channel_image ?? ""; if (dto.link_type == 2) { if (sub_channel > 0) { dto.channel_href = $"/channel/{sub_channel}"; } } if (dto.id == 0) { dto.insert_id = RequestHelper.AdminInfo().Id; dto.insert_time = DateTime.Now; dto = _channelApp.Insert(dto); } else { _channelApp.Update(dto); } ChannelManagerCache.AddChannel(dto); return(Success("保存成功")); }
/// <summary> /// 添加栏目 /// </summary> /// <param name="channel"></param> public static void AddChannel(cms_channel channel) { if (_channels.ContainsKey(channel.id)) { _channels[channel.id] = channel; return; } _channels.TryAdd(channel.id, channel); }
public static int NewChannel(string name, int father) { cms_channel channel = new cms_channel { name = name, fk_father_id = father, enabled = true, has_main = false, brother_index = 0, display_mode = ChannelDisplayMode.ContentList }; LinqHelper.CMS.cms_channel.InsertOnSubmit(channel); LinqHelper.CMS.SubmitChanges(); return(LinqHelper.CMS.cms_channel.Max(x => x.id)); }
/// <summary> /// 获取匹配 /// </summary> /// <param name="channelId"></param> /// <returns></returns> public cms_channel TemplateMatch(int channelId) { var channel = Sqldb.Select <cms_channel>() .Where(s => s.id == channelId).First(s => new cms_channel { channel_name = s.channel_name, channel_template = s.channel_template, content_template = s.content_template }); if (channel == null) { channel = new cms_channel(); } return(channel); }
public IActionResult Save(cms_channel dto) { dto.channel_href = dto.channel_href ?? ""; dto.channel_image = dto.channel_image ?? ""; if (dto.id == 0) { dto.insert_id = RequestHelper.AdminInfo().Id; dto.insert_time = DateTime.Now; _channelApp.Insert(dto); } else { _channelApp.Update(dto); } return(Success("保存成功")); }
/// <summary> ///更新数据 /// </summary> /// <param name="dto"></param> public void Update(cms_channel dto) { Sqldb.Update <cms_channel>().SetSource(dto).IgnoreColumns(s => new { s.insert_time, s.insert_id }).ExecuteAffrows(); }
/// <summary> ///新增数据 /// </summary> /// <param name="dto"></param> public void Insert(cms_channel dto) { Sqldb.Insert(dto).ExecuteAffrows(); }
/// <summary> ///更新数据 /// </summary> /// <param name="dto"></param> public void Update(cms_channel dto) { Sqldb.Update <cms_channel>().SetSource(dto).ExecuteAffrows(); }
/// <summary> /// 添加栏目 /// </summary> /// <param name="channel"></param> public static void AddChannel(cms_channel channel) { _channels[channel.id] = channel; _indexChannels[channel.channel_index] = channel; }
public void TestAccessOrm() { //ORM数据映射 DbConfig.UseDefaultConfig(new TModelDbConfig(GetDbPath())); Console.WriteLine("Start loadding..."); Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToCount()); var cat = new cms_category().Query(m => m.name == "城市").SortAsc(m => m.name).ToModel(); Console.WriteLine(cat.name); //设置只更新部分 //cat.SetPartHandled(); //cat.description = "test"; //cat.Update(m=>m.id == 1); Console.WriteLine(cat.ToValue(m => m.name)); Console.WriteLine(new cms_category().Query(m => m.name == "城市").ToList()[0].name); Console.WriteLine(new cms_category().Query(m => m.name == "城市" && m.id > 0 && m.name == "" || (m.id == 0 || m.name == "")).ToCount()); //指定条件规则查询 Console.WriteLine(new cms_category().Query(m => (m.name == "城市" && (m.id > 0 || m.name == "")) || (m.id == 0 || m.name == "")).ToCount()); var cityList = new List <string> { "城市", "b", "c" }; var layer = new LayerModel { List = cityList }; Console.WriteLine(new cms_category().Query(m => m.name == "城市" || cityList.Contains(m.name) || m.parent_id == Status.Success).ToCount()); Console.WriteLine(new cms_category().Query(m => m.name == "城市" || layer.List.Contains(m.name)).ToCount()); //获取全部 var datsList = new cms_category().Query().ToList(); Console.WriteLine(datsList.Count); //获取N条 datsList = new cms_category().Query().ToList(6); Console.WriteLine(datsList.Count); //获取部分 var partList = new cms_category().Query().ToPartList(6, "id", "name").Select(m => new cms_category { id = int.Parse(m[0]), name = m[1] }).ToList(); Console.WriteLine(partList.Count); //分页查询 var mapper = new cms_category().Query(); var dataCount = mapper.ToCount(); datsList = mapper.ToList(20, 1, dataCount); Console.WriteLine(datsList.Count); //条件拼接查询 mapper.And(m => m.name == "test") .And(m => m.id > 0) .Or(m => m.parent_id > 0); mapper.Or(m => m.parent_id > 0); var channels = new cms_channel().Query().ToList(); Console.WriteLine(channels.Count); var grade = new ucl_grade { id = 5 }; grade.grade_name = "新手1"; var dal = new UclGradeDataAccess(grade); //保持数据库连接 using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect()) { //使用数据库db操作并跟踪实体修改状态 dal.UseDatabase(db).SetPartHandled(); grade.grade = 8; grade.grade_name = "新手"; dal.Update(); } //db销毁后重连数据库 Console.WriteLine(dal.ToValue(m => m.grade_name)); //使用事务(在事务中处理) using (var db = new DbBuilder(new TModelDbConfig(GetDbPath())).KeepConnect()) { try { db.BeginTransaction(); //TODO:something //使用数据库db操作并跟踪实体修改状态 dal.UseDatabase(db).SetPartHandled(); grade.grade = 8; grade.grade_name = "新手"; dal.Update(); db.CommitTransaction(); } catch (Exception ex) { db.RollbackTransaction(); } } //使用事务(批处理事务) var parList = new List <DbParamInfo>(); //添加到批处理事务中,如果执行失败则回滚事务 parList.Add(dal.GetUpdateDbParamInfo().UseVerifyExecResult()); //TODO:添加其他操作到parList var execCount = new DbBuilder(new TModelDbConfig(GetDbPath())).ExecuteSqlTran(parList); Console.WriteLine(execCount); }