各位大侠:
    本人遇到工作难题!不知道.net的在线统计怎么下手.就是统计当前在线的会员数目,每5分钟统计一回,不知道各位大哥有没有做过的或者看到过这方面的资料!会员的登陆方式采用Cookie进行标识的!如果也能统计不是会员的数目更好!
    本人倾仅有100分求解决该问题的方式,有资料,或者代码都行!
    先谢了!

解决方案 »

  1.   

    在数据库中写个job,每5分钟统计一下
      

  2.   

    将登录的信息放在一张内存表中,可放在cache或applicatoin或静态变量中.
    ------说错了莫怪。
      

  3.   

    这个是网上找的统计在线人数的,但这个方法是不准确的。
    新建一个文件global.asax并添加如下代码
    <script Language="C#" runat="server">    void Session_Onstart()
        {
            Application.Lock();
            Application["whoson"] = Convert.ToInt32(Application["whoson"]) + 1;
            Application.UnLock();
        }
        void Session_OnEnd()
        {
            Application.Lock();
            Application["whoson"] = Convert.ToInt32(Application["whoson"]) - 1;
            Application.UnLock();
        }
        public void Application_OnStart(){
            Application.Lock();
            Application["whoson"]=0;
            Application.UnLock();
        }</script>
    ------说错了莫怪。
      

  4.   

    因为Session结束需要很长一段时间。如果要实时统计的话。在用户关闭最后一个窗口的同去让Session失效。或者在内存中建张表保存用户最后的活动时间,用一个xmlHttp隔一段时间去刷新这个值。当检测到某个用户最后时间与当前时间相差一个间隔,表示已经下线,即合对方意外关机也可检测出准确的在线人数。
    ------说错了莫怪。
      

  5.   

    使用Global.aspx统计吧:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Data.SqlClient;
    namespace _016Count_People 
    {
    /// <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=.;database=countpeople;uid=sa;pwd=cyx7688");
    con.Open();
                SqlCommand cmd=new SqlCommand("select * from countpeople",con);
    int count=Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();
             Application["totol"]=count;
    Application["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=.;database=countpeople;uid=sa;pwd=cyx7688");
    con.Open();
    SqlCommand cmd=new SqlCommand("update countpeople set num="+Application["totol"].ToString(),con);
    cmd.ExecuteNonQuery();
    con.Close();
    }

    #region Web 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }
      

  6.   

    首页放置几个LABEL然后显示:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    namespace _016Count_People
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label lblOnLine;
    protected System.Web.UI.WebControls.Label lblTotol;
    protected System.Web.UI.WebControls.Label Label2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    this.lblTotol.Text=Application["totol"].ToString();
    this.lblOnLine.Text=Application["online"].ToString();
    string userName=Request.Form["userName"].ToString();
    string userPwd=Request.Form["userPwd"].ToString();
    SqlConnection con=new SqlConnection("server=.;database=Login;uid=sa;pwd=cyx7688");
    con.Open();
    SqlCommand cmd=new SqlCommand("select Count(*) from login where userName='"+userName+"' and userPwd='"+userPwd+"'",con);
    int count=Convert.ToInt32(cmd.ExecuteScalar());
    if(count>0)
    {   
    Session["flag"]=true;
    Response.Redirect("main.aspx?userName='"+userName);
    }
    else
    {
    Response.Redirect("loginfail.htm");
    }
    con.Close();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion }
    }
      

  7.   

    在数据库中记录,session——start里添加
      

  8.   

    使用Global.aspx统计吧:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;
    using System.Data.SqlClient;
    namespace _016Count_People 
    {
    /// <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=.;database=countpeople;uid=sa;pwd=cyx7688");
    con.Open();
                SqlCommand cmd=new SqlCommand("select * from countpeople",con);
    int count=Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();
             Application["totol"]=count;
    Application["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=.;database=countpeople;uid=sa;pwd=cyx7688");
    con.Open();
    SqlCommand cmd=new SqlCommand("update countpeople set num="+Application["totol"].ToString(),con);
    cmd.ExecuteNonQuery();
    con.Close();
    }

    #region Web 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }
      

  9.   

    仅仅是统计人数就每来一个用户就cache(“num”)加一,走一个减一
    然后建立一个线程,每五分钟轮询一次,然后使用ajax的“推”技术发给管理员就的了
      

  10.   

    <%@ Application Language="C#" %><script runat="server">    void Application_Start(object sender, EventArgs e) 
        {
            // 在应用程序启动时运行的代码
            //Application["userCount"] = 0;
            //Hashtable h = new Hashtable();
            //Context.Cache.Insert("online", h);
            if (Application["Visitor"] == null)
            {
                Application.Lock();
                Application["Visitor"] = 1;
                Application.UnLock();
            }
            else 
            {
                Application.Lock();
                Application["Visitor"] = Int32.Parse(Application["Visitor"].ToString()) + 1;
                Application.UnLock();
            }
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //  在应用程序关闭时运行的代码
            if(Application["Visitor"]!=null)
            {
                Application.Lock();
                Application["Visitor"] = Int32.Parse(Application["Visitor"].ToString()) - 1;
                Application.UnLock();
            }    }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // 在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e) 
        {
            // 在新会话启动时运行的代码
            //Application.Lock();
            //Application["userCount"] = (int)Application["userCount"] + 1;
            //Application.UnLock();
        }    void Session_End(object sender,EventArgs e) 
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。
            //Application.Lock();
            //Application["userCount"] = (int)Application["userCount"] - 1;
            //Application.UnLock();
            //logoutCache();
        }
        //public void logoutCache() 
        //{
        //    Hashtable h = (Hashtable)Context.Cache["online"];
        //    if (h != null)
        //    {
        //        if (h[Session.SessionID] != null)
        //            h.Remove(Session.SessionID);
        //        Context.Cache["online"] = h;
        //    } 
        //}
           
    </script>
      

  11.   

    1. 数据库里建立一个在线用户表: UserOnline
       
       列: id(自增标识列)   Account(帐号)  ActiveTime(活动时间)   备注:当用户是登录用户时, Account 字段记录其帐号,如果不是登录用户,则给 Account 字段编一个你能识别的字符串作为访客的         身份标识。(如用 id(自增标识列) 作为身份标识)
        
       程序读取这个在线用户表来得到在线的人数: select count(distinct account) from UserOnline
    2. 在每一页的 Page_Load 里加上识别用户的代码:
      
       (1) 判断 cookie ,
               如果没有 cookie ,证明这是用户打开的第一个网站里的网页,则给这个用户写一个访客的 cookie。

       cookie 里保存 UserOnline 表里的三个字段。并往 UserOnline 表写一条记录。        如果已经有了 cookie ,则通过 Account(帐号) 来获取用户在 userOnline 表里的记录,并更新其 ActiveTime(活动时间)。
        
    (2) 删除 ActiveTime(活动时间) > 5 的数据。
       
         delete from UserOnline where datediff(minute,ActiveTime,getdate())>5    (3) 获取在线人数:
        select count(distinct account) from UserOnline
      

  12.   

    楼上已经给出了答案,我对上面做了一下总结:http://bbs.zivsoft.com
      

  13.   

    Application或者是在类里面丢一个静态的变量,每来一个人就增加一下静态变量的数值,每走一个人就减少一下那个数值,这样,全局都可以访问那个静态的变量,就能达到统计的作用了,Application也是不错的选择
      

  14.   

    学习------------------------------------
    您的经营之道 http://info.7ecity.com/
    ------------------------------------