网站在线人数以及历史访问人数的统计代码
2019-03-14
准备工作
1、新建一个TongJi 的数据库,添加一个 tongji 的表,在表中有一个 Number 的字段,为 int 类型,Numger初值为1000;
2、新建一个网站;
3、新建数据库连接字符串(
关键代码
4、添加新项/全局应用程序类:Global.asax ,其文件的全部代码如下:
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["TongJiConnectionString"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("select * from tongji", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
Application["total"] = count;
Application["online"] = 0;
}
void Application_End(object sender, EventArgs e)
{