1、新建一个TongJi 的数据库,添加一个 tongji 的表,在表中有一个 Number 的字段,为 int 类型,Number初值为1000;
2、新建一个网站;
3、新建数据库连接字符串( 具体方法)并将其保存到Web.config 文件中, <connectionStrings>节的代码如下:
2、新建一个网站;
3、新建数据库连接字符串( 具体方法)并将其保存到Web.config 文件中, <connectionStrings>节的代码如下:
<
connectionStrings
>
< add name = " TongJiConnectionString " connectionString = " Data Source=.;Initial Catalog=TongJi;Integrated Security=True " providerName = " System.Data.SqlClient " />
</ connectionStrings >
< add name = " TongJiConnectionString " connectionString = " Data Source=.;Initial Catalog=TongJi;Integrated Security=True " providerName = " System.Data.SqlClient " />
</ connectionStrings >
关键代码
4、添加新项/全局应用程序类:Global.asax ,其文件的全部代码如下:
<%
@ Application Language
=
"
C#
"
%>
<% @ Import Namespace = " System.Data.SqlClient " %>
< script runat = " server " >
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)
{
// 在应用程序关闭时运行的代码
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings[ " TongJiConnectionString " ].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand( " update tongji set Number= " + Application[ " total " ].ToString(), con);
cmd.ExecuteNonQuery();
con.Close();
}
void Application_Error( object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start( object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application[ " total " ] = ( int )Application[ " total " ] + 1 ;
Application[ " online " ] = ( int )Application[ " online " ] + 1 ;
Application.UnLock();
}
void Session_End( object sender, EventArgs e)
{
// 在会话结束时运行的代码。
Application.Lock();
Application[ " online " ] = ( int )Application[ " online " ] - 1 ;
Application.UnLock();
}
</ script >
<% @ Import Namespace = " System.Data.SqlClient " %>
< script runat = " server " >
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)
{
// 在应用程序关闭时运行的代码
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings[ " TongJiConnectionString " ].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand( " update tongji set Number= " + Application[ " total " ].ToString(), con);
cmd.ExecuteNonQuery();
con.Close();
}
void Application_Error( object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start( object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application[ " total " ] = ( int )Application[ " total " ] + 1 ;
Application[ " online " ] = ( int )Application[ " online " ] + 1 ;
Application.UnLock();
}
void Session_End( object sender, EventArgs e)
{
// 在会话结束时运行的代码。
Application.Lock();
Application[ " online " ] = ( int )Application[ " online " ] - 1 ;
Application.UnLock();
}
</ script >
运行测试
5、拖两个Lable 到 Default.aspx 上;
6、其Default.aspx.cs 代码如下:
public
partial
class
_Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
this .Label1.Text = " 总访问人数 " + Application[ " total " ].ToString();
this .Label2.Text = " 当前在线数 " + Application[ " online " ].ToString();
}
}
{
protected void Page_Load( object sender, EventArgs e)
{
this .Label1.Text = " 总访问人数 " + Application[ " total " ].ToString();
this .Label2.Text = " 当前在线数 " + Application[ " online " ].ToString();
}
}
7、OK!!启动调试。