利用ASP.NET制作简单计数器的例子

 1using System;
 2using System.Collections;
 3using System.ComponentModel;
 4using System.Web;
 5using System.Web.SessionState;
 6using System.Data.SqlClient;
 7namespace Count 
 8{
 9    /// <summary>
10    /// Global 的摘要说明。
11    /// </summary>

12    public class Global : System.Web.HttpApplication
13    {
14        /// <summary>
15        /// 必需的设计器变量。
16        /// </summary>

17        private System.ComponentModel.IContainer components = null;
18
19        public Global()
20        {
21            InitializeComponent();
22        }
    
23        
24        protected void Application_Start(Object sender, EventArgs e)
25        {
26            SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=xiaohui300;");
27            con.Open();
28            SqlCommand cmd=new SqlCommand("select * from countpeople",con);
29            int count=Convert.ToInt32(cmd.ExecuteScalar());
30            con.Close();
31            Application["totol"]=count;
32            Application["online"]=0;
33        }

34 
35        protected void Session_Start(Object sender, EventArgs e)
36        {
37            Session.Timeout=1;
38            Application.Lock();
39            Application["totol"]=(int)Application["totol"]+1;
40            Application["online"]=(int)Application["online"]+1;
41            Application.UnLock();
42        }

43
44        protected void Application_BeginRequest(Object sender, EventArgs e)
45        {
46
47        }

48
49        protected void Application_EndRequest(Object sender, EventArgs e)
50        {
51
52        }

53
54        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
55        {
56
57        }

58
59        protected void Application_Error(Object sender, EventArgs e)
60        {
61
62        }

63
64        protected void Session_End(Object sender, EventArgs e)
65        {
66            Application.Lock();
67            Application["online"]=(int)Application["online"]-1;
68            Application.UnLock();
69        }

70
71        protected void Application_End(Object sender, EventArgs e)
72        {
73            SqlConnection con=new SqlConnection("server=.;database=countpeople;uid=sa;pwd=xiaohui300;");
74            con.Open();
75            SqlCommand cmd=new SqlCommand("update countpeople set num="+Application["totol"].ToString(),con);
76            cmd.ExecuteNonQuery();
77            con.Close();
78        }

79            
80        Web 窗体设计器生成的代码
90    }

91}

92
93
此页面为Global.aspx全代码
原文地址:https://www.cnblogs.com/icejd/p/364996.html