日期:2014-05-18 浏览次数:20402 次
void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 int count = 0; int day_count = 0; int year = 0; int month = 0; int day = 0; DateTime today = DateTime.Now; SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lianxiConnectionString"].ConnectionString); conn.Open(); SqlDataAdapter sda1 = new SqlDataAdapter("select [count] from [webcount]",conn); DataSet ds = new DataSet(); sda1.Fill(ds); //取总访问数 if (ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; count = Convert.ToInt32(dr["count"].ToString()); } ds.Clear(); SqlDataAdapter sda2 = new SqlDataAdapter("select * from [day_count]",conn); sda2.Fill(ds); //取每天访问数 if (ds.Tables[0].Rows.Count > 0) { DataRow dr = ds.Tables[0].Rows[0]; day_count = Convert.ToInt32(dr["day_count"].ToString()); year = Convert.ToInt32(dr["year"].ToString()); month = Convert.ToInt32(dr["month"].ToString()); day = Convert.ToInt32(dr["day"].ToString()); } //日期不同,则day_count为0 if (today.Year != year || today.Month != month || today.Day != day) day_count = 0; Application["count"] = count; Application["day_count"] = day_count; } void Application_End(object sender, EventArgs e) { // 在应用程序关闭时运行的代码 } void Application_Error(object sender, EventArgs e) { // 在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e) { // 在新会话启动时运行的代码 Application.Lock();//锁定Application int count = 0; int day_count = 0; int year = 0; int month = 0; int day = 0; DateTime today = DateTime.Now; year = today.Year; month = today.Month; day = today.Day; //访问次数都加1 count = (int)Application["count"]; day_count=(int)Application["day_count"]; count++; day_count++; Application["count"] = count; Application["day_count"] = day_count; //更新数据库 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lianxiConnectionString"].ConnectionString); conn.Open(); SqlCommand cmd1 = new SqlCommand("update [webcount] set [count]='"+count+"'",conn); cmd1.ExecuteNonQuery(); SqlCommand cmd2 = new SqlCommand("update [day_count] set [day_count]='"+day_count+"',[year]='"+year+"',[month]='"+month+"',[day]='"+day+"'",conn); cmd2.ExecuteNonQuery(); Application.UnLock();//解锁 } void Session_End(object sender, EventArgs e) { // 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或