写了几行统计网站访问量的代码,当机器重启或关机开机后访问量重置为零,如何使访问量不丢失?
 if (application.getAttribute("count") == null) {
     application.setAttribute("count", new Integer(0));
   }
Integer count = (Integer) application.getAttribute("count");
   application.setAttribute("count", new Integer(count.intValue() + 1));

解决方案 »

  1.   

    你在数据库专门建个表,加个字段count的,然后在Global.asax里把它存起来,如下:
    void Application_Start(object sender, EventArgs e) 
        {
            string sql = "select count from tongji";
            SqlConnection con = new SqlConnection("uid=sa;pwd=;DataBase=test;Server=(local);");
            con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
            Application["total"] = count;//历史访问人数
            Application["online"] = 0;//在线人数
            // 在应用程序启动时运行的代码    }
        
        void Application_End(object sender, EventArgs e) 
        {
            string sql = "update tongji set count=" + int.Parse(Application["total"].ToString()) + "";
            SqlConnection con = new SqlConnection("uid=sa;pwd=;DataBase=test;Server=(local);");
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
            //  在应用程序关闭时运行的代
        }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // 在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e) 
        {
            // 在新会话启动时运行的代码
            Application.Lock();
            Application["total"] = (int)Application["total"] + 1;
            Application["online"] = (int)Application["online"] + 1;
            Application.UnLock();    }    void Session_End(object sender, EventArgs e) 
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。
            Application.Lock();
            Application["online"] = (int)Application["online"] - 1;
            Application.UnLock();    }