日期:2014-05-17  浏览次数:20589 次

asp.net 文章浏览次数??
本意是单击文章标题时转入显示文章内容页同时使文章的浏览次数加1 (浏览次数是作为字段存储在数据库中的)可是运行时数据库中的所有文章都会自加1;刚学习asp.net不久 迷惑了,请问各位是怎么回事??坐等。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace _17淘学网
{
  public partial class shownew : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {  
  int id = Convert.ToInt32(Request.Params["id"]);



  if (!Page.IsPostBack)
  { 

  int scount=0;
  scount = Count(id);
  scount+=1;
  write(scount, id);
   
  listbind(id); }

   
  }

  void listbind(int id)//绑定数据并显示
  { string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source="+ Server.MapPath("info.accdb");
  DataSet ds = new DataSet();

  using (OleDbConnection Dconn = new OleDbConnection(strConnection))
  {
  OleDbDataAdapter dapter = new OleDbDataAdapter("select * from info where Id="+id, Dconn);
  dapter.Fill(ds, "infostu");

  }
  DataList1.DataSource = ds.Tables["infostu"].DefaultView;
  DataList1.DataBind();



 }
  int Count(int cid)//读出相应记录中的浏览次数字段中的值
  {
   
  string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=" + Server.MapPath("info.accdb");

  OleDbConnection constr = new OleDbConnection(strConnection);

  OleDbCommand command = new OleDbCommand();
  command.CommandText = "select [SeeCount] from [info] where Id="+cid;

  command.Connection = constr;
  constr.Open();
  OleDbDataReader reader = command.ExecuteReader();

  while (reader.Read())
  { cid=reader.GetInt32(0); }
  command = null;
  constr.Close();
  constr = null;
  return cid;
  }
  void write(int count,int id)//更新数据库浏览次数字段
  {
  string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=" + Server.MapPath("info.accdb");


  //建立数据库连接对象
  OleDbConnection sqlconn = new OleDbConnection(strConnection);
  //打开连接


  OleDbCommand sqlmannd = new OleDbCommand();
  sqlmannd.Connection = sqlconn;
  sqlmannd.CommandText = "update info set SeeCount=@SeeCount where Id+ "+id;
  //关闭连接
  sqlmannd.Parameters.AddWithValue("@SeeCount", count);
  sqlconn.Open();
  sqlmannd.ExecuteNonQuery();
   
  sqlmannd = null;
  sqlconn.Close();
  sqlconn = null;
  }
  }
}

------解决方案--------------------
加1直接写
sqlmannd.CommandText = "update info set SeeCount=SeeCount+1 where Id="+id;
就可以了
------解