熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net實現頁面訪問次數統計

2022-06-13   來源: .NET編程 

  數據庫准備:建立一個表total裡面數據項為totals類型為varchar

  語言環境:C#

  globalasax裡的代碼

  <%@ Import Namespace=SystemData %>

  <%@ Import Namespace=SystemDataSqlClient %>

  <script language=C# runat=server>

  string strSelect;

  SqlConnection conPubs;

  SqlDataAdapter dadPubs;

  DataSet dstTitles;

  DataRow drowTitle;

  void Session_Start(Object sender EventArgs e)

  {

  if ( Application[ SessionCount ] == null ) {

  Application[ SessionCount ] = ;

  strSelect = SELECT totals From total;

  conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

  dadPubs = new SqlDataAdapter(strSelect conPubs);

  dstTitles = new DataSet();

  dadPubsFill(dstTitles total);

  drowTitle = dstTitlesTables[total]Rows[];

  Application[ SessionCount ]=SystemConvertToInt(drowTitle[totals]ToString()Trim());

  }

  }

  void Session_End() {

  Application[SessionCount] = ;

  }

  </script>

  ============================

  SessionCountaspx裡的代碼

  void Page_Load(Object sender EventArgs e)

  {

  int total = ;

  string strSelect;

  SqlConnection conPubs;

  //要執行某項數據操作要用SqlCommand方式調用

  SqlCommand cmdSql;

  //為了防止同文檔裡的其他頁面在訪問時也進行累加運算

  int intHits = ;

  intHits = (int)Application[SessionCount];

  intHits += ;

  Application[SessionCount] = intHits;

  lblSessionCountText = Application[ SessionCount ]ToString();

  total = (int)Application[SessionCount];

  strSelect = update total set totals= @total;

  conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

  cmdSql = new SqlCommand(strSelect conPubs);

  cmdSqlParametersAdd(@total total);

  conPubsOpen();

  cmdSqlExecuteNonQuery();

  conPubsClose();

  }

  ================上段代碼有個小問題就是過了一段時間後Application[SessionCount]的值會變成而且由於前面設置了一個初始的也會連帶的把數據庫裡原來保存的值更新為起始

  更改後

  globalasax

  <%@ Import Namespace=SystemData %>

  <%@ Import Namespace=SystemDataSqlClient %>

  <script language=C# runat=server>

  string strSelect;

  SqlConnection conPubs;

  SqlDataAdapter dadPubs;

  DataSet dstTitles;

  DataRow drowTitle;

  void Session_Start(Object sender EventArgs e)

  {

  if ( Application[ SessionCount ] == null ) {

  Application[ SessionCount ] = ;

  strSelect = SELECT totals From total;

  conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

  dadPubs = new SqlDataAdapter(strSelect conPubs);

  dstTitles = new DataSet();

  dadPubsFill(dstTitles total);

  drowTitle = dstTitlesTables[total]Rows[];

  Application[ SessionCount ]=SystemConvertToInt(drowTitle[totals]ToString()Trim());

  }

  }

  void Session_End() {

  Application[SessionCount] = null;

  }

  </script>

  

  SessionCountaspx

  <script language=C# runat=server>

  void Page_Load(Object sender EventArgs e)

  {

  int total = ;

  string strSelect;

  SqlConnection conPubs;

  //要執行某項數據操作要用SqlCommand方式調用

  SqlCommand cmdSql;

  //為了防止同文檔裡的其他頁面在訪問時也進行累加運算

  int intHits = ;

  intHits = (int)Application[SessionCount];

  intHits += ;

  total = intHits;

  lblSessionCountText = intHitsToString();

  strSelect = update total set totals= @total;

  conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

  cmdSql = new SqlCommand(strSelect conPubs);

  cmdSqlParametersAdd(@total total);

  conPubsOpen();

  cmdSqlExecuteNonQuery();

  conPubsClose();

  Application[SessionCount] = null;

  }

  </script>


From:http://tw.wingwit.com/Article/program/net/201311/13438.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.