统计登录人数

在数据库里面建一张表VisitInfor,字段为:VisitCount

1.新建一个类文件:VisitInforDAL.cs

代码:

/// <summary>

///VisitInforDAL

/// </summary>

public class VisitInforDAL

{

    private string strConn = ConfigurationManager.ConnectionStrings["cnMobilePhoneShop"].ToString();

    protected SqlConnection conn;

    public VisitInforDAL()

    {

        //TODO: 在此处添加构造函数逻辑

        conn = new SqlConnection(strConn);

    }

    public void UpdateVisitInfor(string count)

    {

        SqlCommand myCm = new SqlCommand(string.Format("UPDATE[VisitInfor] SET [VisitCount] ={0}", count), conn);

        try

        {

            conn.Open();

            myCm.ExecuteNonQuery();

            myCm.Dispose();

            //myCm.Close();

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }

    public int GetVisitCount()

    {

        SqlCommand myCm = new SqlCommand("SELECT [VisitCount]FROM [VisitInfor]", conn);

        try

        {

            int count;

            conn.Open();

            count = Convert.ToInt32(myCm.ExecuteScalar());

            myCm.Dispose();

            conn.Close();

            return count;

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }

}

 

 

 

 

2.添加一个Global.asax文件

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)

    {

        //在应用程序启动时运行的代码

        VisitInforDAL visitInforDAL = new VisitInforDAL();

        int Count = visitInforDAL.GetVisitCount();

        Application.Lock();

        Application["Counter"] = Count;

        Application["OnLineCounter"] = 0;

        Application.UnLock();

    }

    void Application_End(object sender, EventArgs e)

    {

        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

        VisitInforDAL visitInforDAL = new VisitInforDAL();

        visitInforDAL.UpdateVisitInfor(Application["Counter"].ToString());

    }

    void Application_Error(object sender, EventArgs e)

    {

        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)

    {

        //在新会话启动时运行的代码

        Application.Lock();

        Application["Counter"] = (int)Application["Counter"] + 1;

        Application["OnlineCounter"] = (int)Application["OnlineCounter"] + 1;

        Application.UnLock();

    }

 

    void Session_End(object sender, EventArgs e)

    {

        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

        Application.Lock();

        Application["OnlineCounter"] = (int)Application["OnlineCounter"] - 1;

        Application.UnLock();

 

    }

      

</script>

 

3.在页面上添加两个label(总访问量和在线人数)

界面后台代码:

public partial class UserControl_bottom : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

        this.Label1.Text = Application["Counter"].ToString();

        this.Label2.Text = Application["OnlineCounter"].ToString();

    }

}

posted @ 2013-01-23 13:48  acmzs  阅读(254)  评论(0编辑  收藏  举报