/// <summary> /// insert link /// </summary> /// <param name="link"></param> /// <returns></returns> public int Insert(LinkInfo link) { string cmdText = string.Format(@"insert into [{0}links] ( [type],[linkname],[linkurl],[position],[target],[description],[sortnum],[status],[createtime] ) values ( @type,@linkname,@linkurl,@position,@target,@description,@sortnum,@status,@createtime )", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { conn.Execute(cmdText, new { Type = link.Type, LinkName = link.LinkName, LinkUrl = link.LinkUrl, Postion = link.Position, Target = link.Target, Description = link.Description, SortNum = link.SortNum, Status = link.Status, CreateTime = link.CreateTime }); return conn.Query<int>(string.Format("select [linkid] from [{0}links] order by [linkid] desc limit 1", ConfigHelper.Tableprefix), null).First(); } }
/// <summary> /// 根据日志ID删除评论 /// </summary> /// <param name="postId">日志ID</param> /// <returns></returns> public int DeleteCommentByPost(int postId) { string cmdText = string.Format("delete from [{0}comments] where [postId] = @postId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { postId = postId }); } }
/// <summary> /// 删除 /// </summary> /// <param name="userid"></param> /// <returns></returns> public int Delete(UserInfo userinfo) { string cmdText = string.Format("delete from [{0}users] where [userid] = @userid", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new {userid = userinfo.UserId }); } }
/// <summary> /// 是否存在此用户名 /// </summary> /// <param name="userName"></param> /// <returns></returns> public bool ExistsUserName(string userName) { string cmdText = string.Format("select count(1) from [{0}users] where [userName] = @userName ", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new {userName = userName })>0; } }
/// <summary> /// delete link /// </summary> /// <param name="link"></param> /// <returns></returns> public int Delete(LinkInfo link) { string cmdText = string.Format("delete from [{0}links] where [linkid] = @linkid", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { categoryid = link.LinkId }); } }
public int Delete(TagInfo tag) { string cmdText = string.Format("delete from [{0}category] where [categoryid] = @categoryid", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { categoryid = tag.TagId }); } }
public LinkInfo GetById(object Id) { string cmdText = string.Format("select * from [{0}links] where [linkid]=@linkid ", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Query<LinkInfo>(cmdText, new {linkid=(int)Id }).First(); } }
/// <summary> /// 删除 /// </summary> /// <param name="commentId"></param> /// <returns></returns> public int Delete(CommentInfo comment) { string cmdText = string.Format("delete from [{0}comments] where [commentId] = @commentId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { commentId = comment.CommentId }); } }
/// <summary> /// 获取用户 /// </summary> /// <param name="Id"></param> /// <returns></returns> public UserInfo GetById(object Id) { string cmdText = string.Format("select * from [{0}users] where [UserId] = @userId ", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<UserInfo>(cmdText, new { userId = (int)Id }); return list.ToList().Count > 0 ? list.First() : null; } }
public TagInfo GetTagByPageName(string pagename) { string cmdText = string.Format("select * from [{0}category] where [PageName] = @PageName", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<TagInfo>(cmdText, new { PageName = pagename }); return list.ToList().Count > 0 ? list.ToList()[0] : null; } }
/// <summary> /// 获取评论 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public CommentInfo GetById(object id) { string cmdText = string.Format("select * from [{0}comments] where [commentId] = @commentId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<CommentInfo>(cmdText, new { commentId = (int)id }); return list.ToList().Count > 0 ? list.ToList()[0] : null; } }
public void TestMethod2() { string cmdText = "select p.* from [jq_posts] p where postid=340"; using (var conn = new DapperHelper().OpenConnection(ConnectionString)) { var post = conn.Query<PostInfo>(cmdText).First(); conn.Execute("update jq_posts set updatetime =@updatetime where postid=340",new{updatetime = DateTime.Now.ToString()}); } }
/// <summary> /// 删除文章 /// </summary> /// <param name="post"></param> public virtual int Delete(PostInfo post) { PostInfo oldPost = GetById(post.PostId); if (oldPost == null) throw new Exception("文章不存在"); string cmdText = string.Format("delete from [{0}posts] where [PostId] = @PostId", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new {PostId = post.PostId }); } }
/// <summary> /// 获取分类 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public CategoryInfo GetById(object id) { if ((int)id <= 0) { return null; } string cmdText = string.Format("select * from [{0}category] where [categoryid] = @categoryid", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<CategoryInfo>(cmdText, new { categoryid = (int)id }); return list.ToList().Count > 0 ? list.First() : null; } }
/// <summary> /// 更新统计数据 /// </summary> /// <param name="statistics"></param> /// <returns></returns> public bool UpdateStatistics(StatisticsInfo statistics) { string cmdText = string.Format("update [{0}sites] set PostCount=@PostCount,CommentCount=@CommentCount,VisitCount=@VisitCount,TagCount=@TagCount", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { PostCount = statistics.PostCount, CommentCount = statistics.CommentCount, VisitCount = statistics.VisitCount, TagCount = statistics.TagCount })>0; } }
/// <summary> /// 获取统计数据 /// </summary> /// <returns></returns> public StatisticsInfo GetStatistics() { string cmdText = string.Format("select * from [{0}sites] limit 1", ConfigHelper.Tableprefix); string insertText = string.Format("insert into [{0}sites] ([PostCount],[CommentCount],[VisitCount],[TagCount]) values ( '0','0','0','0')", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<StatisticsInfo>(cmdText,null).ToList(); if (list.Count == 0) { conn.Execute(insertText,null); } return list.Count > 0 ? list[0] : null; } }
/// <summary> /// 检查别名是否重复 /// </summary> /// <returns></returns> public virtual void CheckPageName(PostInfo post) { if (string.IsNullOrEmpty(post.PageName)) { return; } while (true) { string cmdText = post.PostId == 0 ? string.Format("select count(1) from [{1}posts] where [pagename]='{0}' ", post.PageName, ConfigHelper.Tableprefix) : string.Format("select count(1) from [{2}posts] where [pagename]='{0}' and [postid]<>{1}", post.PageName, post.PostId, ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { int r = conn.Query<int>(cmdText,null).First(); if (r == 0) { return; } } post.PageName += "-2"; } }
public void TestMethod1() { //var connection = new DapperHelper().OpenConnection(); //var sql = @"select q.*,t.typeid,t.typename from ems_Question q,ems_Questiontype t where t.typeid=q.typeid"; //var data = connection.Query<Question, QuestionType, Question>(sql, (quest, type) => //{ // quest.QuestionType = type; // return quest; //}, splitOn: "typeid,qstid"); //foreach (Question q in data) //{ // Console.WriteLine(q.QstDescription + " " + q.QuestionType.TypeName); //} //connection.Close(); // string cmdText = string.Format("select p.*,u.UserId,u.UserName,u.NickName from [{0}posts] p,[{0}Users] u where p.UserId=u.UserId ", ConfigHelper.Tableprefix); // using (var conn = new DapperHelper().OpenConnection(ConnectionString)) string cmdText = string.Format("select p.*,u.UserId,u.UserName,c.CategoryId,c.CateName from [{0}posts] p,[{0}Users] u,[{0}category] c where p.PostId=@PostId and p.UserId=u.UserId and p.CategoryId=c.CategoryId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection(ConnectionString)) { // var list = conn.Query<PostInfo>(cmdText, new { PostId = (int)id }); // return list.Any() ? list.ToList()[0] : null; var list = conn.Query<PostInfo, UserInfo, CategoryInfo, PostInfo>(cmdText, (post, user, cate) => { post.Author = user; post.Category = cate; return post; }, new { PostId = 384 }, splitOn: "UserId,CategoryId"); foreach (var postInfo in list) { Console.WriteLine(postInfo.PostId + " " + postInfo.Title); } } }
/// <summary> /// 获取实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual PostInfo GetById(object id) { //string cmdText = string.Format("select p.PostId as Id,p.Title,p.CategoryId,p.TitlePic,p.PageName,u.UserId,u.UserName,u.Role,u.NickName from [{0}posts] p,[{0}Users] u where p.UserId=u.UserId limit 1", ConfigHelper.Tableprefix); //using (var conn = new DapperHelper().OpenConnection()) //{ // // var list = conn.Query<PostInfo>(cmdText, new { PostId = (int)id }); // // return list.Any() ? list.ToList()[0] : null; // var list = conn.Query<PostInfo, UserInfo, PostInfo>(cmdText, (post, user) => // { // post.Author = user; // return post; // }, splitOn: "UserId"); // return list.Any() ? list.ToList()[0] : null; //} string cmdText = string.Format("select p.*,u.*,c.* from [{0}posts] p,[{0}Users] u,[{0}category] c where p.PostId=@PostId and p.UserId=u.UserId and p.CategoryId=c.CategoryId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<PostInfo, UserInfo, CategoryInfo, PostInfo>(cmdText, (post, user, cate) => { post.Author = user; post.Category = cate; return post; }, new { PostId = (int)id }, splitOn: "UserId,CategoryId"); return list.Any() ? list.ToList()[0] : null; } }
/// <summary> /// 获取文章归档统计 /// </summary> /// <returns></returns> public virtual List<ArchiveInfo> GetArchive() { string cmdText = string.Format("select format(PostTime, 'yyyymm') as [date] , count(*) as [count] from [{0}posts] where [status]=1 and [PostStatus]=0 group by format(PostTime, 'yyyymm') order by format(PostTime, 'yyyymm') desc", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Query<ArchiveInfo>(cmdText,null).ToList(); } }
/// <summary> /// 更新访问量 /// </summary> /// <param name="postId"></param> /// <param name="addCount"></param> /// <returns></returns> public virtual int UpdatePostViewCount(int postId, int addCount) { string cmdText = string.Format("update [{0}posts] set [viewcount] = [viewcount] + @addcount where [postid]=@postid", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, new { addcount =addCount,postid = postId }); } }
/// <summary> /// 新增文章 /// </summary> /// <param name="post"></param> /// <returns></returns> public virtual int Insert(PostInfo post) { CheckPageName(post); string cmdText = string.Format(@"insert into [{0}posts] ( [CategoryId],[TitlePic],[Title],[Summary],[PostContent],[PageName],[UserId],[CommentStatus],[CommentCount],[ViewCount],[Tag],[UrlFormat],[Template],[Recommend],[Status],[TopStatus],[HomeStatus],[PostTime],[UpdateTime] ) values ( @CategoryId,@TitlePic,@Title,@Summary,@PostContent,@PageName,@UserId,@CommentStatus,@CommentCount,@ViewCount,@Tag,@UrlFormat,@Template,@Recommend,@Status,@TopStatus,@HomeStatus,@PostTime,@UpdateTime )", ConfigHelper.Tableprefix); using(var conn = new DapperHelper().OpenConnection()) { conn.Execute(cmdText, post); return conn.Query<int>(string.Format("select [PostId] from [{0}Posts] order by [PostId] desc limit 1", ConfigHelper.Tableprefix), null).First(); } }
/// <summary> /// 更新文章 /// </summary> /// <param name="post"></param> public virtual int Update(PostInfo post) { string cmdText = string.Format(@"update [{0}posts] set [CategoryId]=@CategoryId, [TitlePic]=@TitlePic, [Title]=@Title, [Summary]=@Summary, [PostContent]=@PostContent, [PageName]=@PageName, [UserId]=@UserId, [CommentStatus]=@CommentStatus, [CommentCount]=@CommentCount, [ViewCount]=@ViewCount, [Tag]=@Tag, [UrlFormat]=@UrlFormat, [Template]=@Template, [Recommend]=@Recommend, [Status]=@Status, [TopStatus]=@TopStatus, [HomeStatus]=@HomeStatus, [PostTime]=@PostTime, [UpdateTime]=@UpdateTime where [PostId]=@PostId", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, post); } }
/// <summary> /// 获取分页列表 /// </summary> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="recordCount"></param> /// <param name="categoryId"></param> /// <param name="tagId"></param> /// <param name="userId"></param> /// <param name="recommend"></param> /// <param name="status"></param> /// <param name="topstatus"></param> /// <param name="PostStatus"></param> /// <param name="begindate"></param> /// <param name="enddate"></param> /// <param name="keyword"></param> /// <returns></returns> public virtual List<PostInfo> GetPostList(int pageSize, int pageIndex, out int recordCount, string categoryId, int tagId, int userId, int recommend, int status, int topstatus, int PostStatus, string begindate, string enddate, string keyword) { string condition = " 1=1 "; if (categoryId != ""&&categoryId!="-1") { condition += " and categoryId in(" + categoryId+")"; } if (tagId != -1) { condition += " and tag like '%{" + tagId + "}%'"; } if (userId != -1) { condition += " and userid=" + userId; } if (recommend != -1) { condition += " and recommend=" + recommend; } if (status != -1) { condition += " and status=" + status; } if (topstatus != -1) { condition += " and topstatus=" + topstatus; } if (PostStatus != -1) { condition += " and PostStatus=" + PostStatus; } if (!string.IsNullOrEmpty(begindate)) { condition += " and PostTime>=#" + begindate + "#"; } if (!string.IsNullOrEmpty(enddate)) { condition += " and PostTime<#" + enddate + "#"; } if (!string.IsNullOrEmpty(keyword)) { condition += string.Format(" and (summary like '%{0}%' or title like '%{0}%' )", keyword); } using (var conn = new DapperHelper().OpenConnection()) { string cmdTotalRecord = "select count(1) from [" + ConfigHelper.Tableprefix + "posts] where " + condition; recordCount = conn.Query<int>(cmdTotalRecord,null).First(); string cmdText = new DapperHelper().GetPageSql("[" + ConfigHelper.Tableprefix + "Posts]", "[PostId]", "*", pageSize, pageIndex, 1, condition); var list = conn.Query<PostInfo>(cmdText, null); return list.ToList(); } }
/// <summary> /// 获取相关文章 /// </summary> /// <param name="postId"></param> /// <param name="rowCount"></param> /// <returns></returns> public virtual List<PostInfo> GetPostListByRelated(int postId, int rowCount) { string tags; PostInfo post = GetById(postId); if (post != null && post.Tag.Length > 0) { tags = post.Tag; tags = tags.Replace("}", "},"); string[] idList = tags.Split(','); string where = idList.Where(tagId => !string.IsNullOrEmpty(tagId)).Aggregate(" (", (current, tagId) => current + string.Format(" [tags] like '%{0}%' or ", tagId)); where += " 1=2 ) and [status]=1 and [postid]<>" + postId; string cmdText = string.Format("select * from [{2}posts] where {1} order by [postid] desc limit {0}", rowCount, where, ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<PostInfo>(cmdText, null); return list.ToList(); } } return new List<PostInfo>(); }
/// <summary> /// 根据pagename获取实体 /// </summary> /// <param name="pagename"></param> /// <returns></returns> public virtual PostInfo GetPostbyPageName(string pagename) { string cmdText = string.Format("select * from [{0}posts] where [pagename] = @_pagename limit 1", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<PostInfo>(cmdText, new { _pagename = pagename }); return list.ToList().Count > 0 ? list.ToList()[0] : null; } }
/// <summary> /// 统计评论 /// </summary> /// <param name="userId"></param> /// <param name="postId"></param> /// <param name="incChild"></param> /// <returns></returns> public int GetCommentCount(int userId, int postId, bool incChild) { string condition = " 1=1 "; if (userId != -1) { condition += " and [userId] = " + userId; } if (postId != -1) { condition += " and [postId] = " + postId; } if (incChild == false) { condition += " and [parentid]=0"; } using (var conn = new DapperHelper().OpenConnection()) { string cmdTotalRecord = "select count(1) from [" + ConfigHelper.Tableprefix + "comments] where " + condition; return conn.Query<int>(cmdTotalRecord, null).First(); } }
/// <summary> /// 获取文章评论 /// </summary> /// <param name="postid"></param> /// <returns></returns> public virtual List<CommentInfo> GetCommentsByPost(int postid) { string cmdText = string.Format("select * from [{0}comments] where postid=@PostId order by createtime desc", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { var list = conn.Query<CommentInfo>(cmdText, new {PostId = postid}); return list!=null ? list.ToList() : null; } }
/// <summary> /// 添加 /// </summary> /// <param name="comment"></param> /// <returns></returns> public int Insert(CommentInfo comment) { string cmdText = string.Format(@"insert into [{0}comments]( PostId, ParentId,UserId,Author,Email,AuthorUrl,Contents,EmailNotify,IpAddress,CreateTime,Approved) values ( @PostId, @ParentId,@UserId,@Author,@Email,@AuthorUrl,@Contents,@EmailNotify,@IpAddress,@CreateTime,@Approved)", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { conn.Execute(cmdText, comment); return conn.Query<int>(string.Format("select [CommentId] from [{0}comments] order by [CommentId] desc limit 1", ConfigHelper.Tableprefix), null).First(); } }
/// <summary> /// 更新 /// </summary> /// <param name="comment"></param> /// <returns></returns> public int Update(CommentInfo comment) { string cmdText = string.Format(@"update [{0}comments] set PostId=@PostId, ParentId=@ParentId, UserId=@UserId, Author=@Author, Email=@Email, AuthorUrl=@AuthorUrl, Contents=@Contents, EmailNotify=@EmailNotify, IpAddress=@IpAddress, CreateTime=@CreateTime, Approved=@Approved where CommentId=@CommentId ", ConfigHelper.Tableprefix); using (var conn = new DapperHelper().OpenConnection()) { return conn.Execute(cmdText, comment); } }