给BlogEngine增加日志点击数

in Develop

有朋友在留言时提到,所以这里放出来。
我用的是sqlserver,如果是xml就得改相应的代码了。
先在数据表中增加一字段:hit,给post.cs中增加一Hit字段。
同时更改provider的代码,这里就不给出来了。


blogservice.cs添加UpdateHit(guid id)方法
public static void UpdateHit(guid id)
{
LoadProviders();
_provider.UpdateHit(id);
}

sqldataprovider.cs
public override void UpdateHit(guid id)
{
string sqlQuery = " update dbo.be_Posts set hit = hit+1 where postid=@id";
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sqlQuery, new SqlParameter("@id", id));
}

post.cs
public static void UpdateHit(guid id)
{
Posts.Find(delegate(Post p) { return p.Id== id; }).Hit++; //更新内存数据
BlogService.UpdateHit(id); //更新数据库记录
}

post.aspx.cs

if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
{
Guid id = new Guid(Request.QueryString["id"]);
this.Post = Post.GetPost(id);

if (Post != null)
{
Post.UpdateHit(this.Post.Id); //增加
......................

更改完毕。

7 Comments

7 Comments

  1. :p,总感觉还有改进的地方,先收录回去慢慢研究下!

  2. @蚂蚁线:
    可以写扩展来实现。

  3. 我用的是XmlProvider,如果加入这个统计,每次访问都需要重写下那个XML文件,那IO也太频繁了.
    还是不改了

  4. static 不少。。哇哈哈^_^

  5. 好代码,多谢共享!:up:

  6. 学习下啊
    以后自己也开个独立博客来玩玩!
    要是不用改这么多代码,就好用多了

  7. 本起用用这个的。。想来还是不用了。。。没啥多大的意义。。仅仅是让自已晓得有多人点击了而已已。。

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>