網站流量代碼

global.asax.as

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Data.SqlClient;
namespace login
{
 /// <summary>
 /// Global 的摘要說明。
 /// </summary>
 public class Global : System.Web.HttpApplication
 {
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private System.ComponentModel.IContainer components = null;

  public Global()
  {
   InitializeComponent();
  } 
  
  protected void Application_Start(Object sender, EventArgs e)//開始
  {//連接數據庫
   SqlConnection con=new  SqlConnection("server=(local);database=test;uid=sa;pwd=;");
   con.Open();
   SqlCommand cmd=new SqlCommand("select *from count",con);
   int  count=Convert.ToInt32(cmd.ExecuteScalar());//返回首行首列
    
                        Application.Add("totol",count);//賦初值

   Application.Add("online",0);
    

  }
 
  protected void Session_Start(Object sender, EventArgs e)
  { // Session.Timeout=1;  //設定過期時間 一般不用設置  
     Application.Lock();//關閉會話 解決同時發生的情況
                  Application["totol"]=(int)Application["totol"]+1;//總的訪問量加一
    Application["online"]=(int)Application["online"]+1;//當前在線人數加一
               Application.UnLock();
  }

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_EndRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_Error(Object sender, EventArgs e)
  {

  }

  protected void Session_End(Object sender, EventArgs e)//對話結束
  {       Application.Lock();//鎖
                        Application["online"]=(int)Application["online"]-1;//當前在線人數在關閉會話是減一
   Application.UnLock();
 
  }

  protected void Application_End(Object sender, EventArgs e)
  { //把訪問量更新到數據庫的歷史訪問量的值
       SqlConnection con=new  SqlConnection("server=(local);database=test;uid=sa;pwd=;");
       con.Open();
       SqlCommand cmd=new SqlCommand("update count set num="+Application["totol"].ToString(),con);//把當前totol 的值更新到數據庫上
             cmd.ExecuteNonQuery();//開始執行更新
       con.Close();//關閉

     
                  
  }
   
  #region Web 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.components = new System.ComponentModel.Container();
  }
  #endregion
 }
}


顯示流量頁面:
 private void Page_Load(object sender, System.EventArgs e)
  {
            this.lbltotol.Text=Application["totol"].ToString(); //把totol的值賦給lbltotol並顯示出來
   this.lblonline.Text=Application["online"].ToString();//把online當前在線的值賦給lblonline並顯示出來

   
   
   // 在此處放置用戶代碼以初始化頁面
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章