asp.net在线人数和历史访问总人数代码
要想实现历史人数访问需使用数据库存储以前的访问人数.
在global.asax 中要实现命名空间的应用可使用
<%@Import Namespace= "System.Data " %>
<%@Import Namespace= "System.Data.SqlClient " %>
也可直接按下面的写,但可能不太方便
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("select * from num",con);
int total = Convert.ToInt32(com.ExecuteScalar()); //获取以前访问人数
con.Close();
Application["total"] = total;
Application.Add("num",0);//存在线人数
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("update num set num="+Application["total"].ToString(),con);
com.ExecuteNonQuery();
con.Close();
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application["total"] =(int)Application["total"] + 1;
Application["num"] = (int)Application["num"] + 1;
Application.UnLock();
}