在Global.asax中我按如下输入代码~进去WEB页面时查找SQL调出数据,重启应用程序时对它保存,可是我在SQL中对数据做出修改时,却无法在WEB页面中显示出来比如:在SQL中的Vistiors是0,我第一次进入WEB页面是1,第二次是2,这时候SQL中却还是0
接着我退出Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,于是我又进入Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,接下来我按F5运行程序WEB页面中显示3。这时候我再看SQL中的vistiors变成了2.不管我怎么改SQL中的vistiors, web页面中的值都不会按我想的改变这是为什么啊?望高手指点一二,在下不圣感激!!!!
protected void Application_Start(Object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand("select Visitors from Counter",con);
con.Open();
try
{
Application["counter"]=(int)cmd.ExecuteScalar();
}
finally
{
con.Close();
}
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["counter"] = ((int)Application["counter"]) + 1;
Application.UnLock();
}
protected void Application_End(Object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand("update Counter set Visitors=" + Application["counter"].ToString(),con);
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
接着我退出Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,于是我又进入Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,接下来我按F5运行程序WEB页面中显示3。这时候我再看SQL中的vistiors变成了2.不管我怎么改SQL中的vistiors, web页面中的值都不会按我想的改变这是为什么啊?望高手指点一二,在下不圣感激!!!!
protected void Application_Start(Object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand("select Visitors from Counter",con);
con.Open();
try
{
Application["counter"]=(int)cmd.ExecuteScalar();
}
finally
{
con.Close();
}
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["counter"] = ((int)Application["counter"]) + 1;
Application.UnLock();
}
protected void Application_End(Object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand("update Counter set Visitors=" + Application["counter"].ToString(),con);
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
http://IP或者域名/xxx.aspx这样的显示进行测试
在SQL中的Vistiors是0,我第一次进入WEB页面是1,第二次是2,这时候SQL中却还是0
接着我退出Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,于是我又进入Microsoft Visual Studio .NET 2003 在SQL中的Vistiors还是0,接下来我按F5运行程序WEB页面中显示3。这时候我再看SQL中的vistiors变成了2.这个过程中的Vistiors值是保存在哪里的~
请求 ASP.NET 应用程序中第一个资源(如页)时调用。在应用程序的生命周期期间仅调用一次 Application_Start 方法。可以使用此方法执行启动任务,如将数据加载到缓存中以及初始化静态值。在应用程序启动期间应仅设置静态数据。由于实例数据仅可由创建的 HttpApplication 类的第一个实例使用,所以请勿设置任何实例数据。
Application_End
在卸载应用程序之前对每个应用程序生命周期调用一次。
Session_OnStart
如果请求开始一个新会话,Session_OnStart 子例程会在请求开始时运行。如果请求不包含 SessionID 值或请求所包含的 SessionID 属性引用一个已过期的会话,则会开始一个新会话。
2,刷新页面如果不是新开窗口,是看作一个Session的