时间:2021-05-28
数据库准备:建立一个表total里面数据项为totals类型为varchar 50
.net语言环境:C#
global.asax里的代码
复制代码 代码如下:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
string strSelect;
SqlConnection conPubs;
SqlDataAdapter dadPubs;
DataSet dstTitles;
DataRow drowTitle;
void Session_Start(Object sender , EventArgs e)
{
if ( Application[ "SessionCount" ] == null ) {
Application[ "SessionCount" ] = 0;
strSelect = "SELECT totals From total";
conPubs = new SqlConnection(@"Server=localhost;Integrated Security=SSPI;Database=test");
dadPubs = new SqlDataAdapter(strSelect, conPubs);
dstTitles = new DataSet();
dadPubs.Fill(dstTitles, "total");
drowTitle = dstTitles.Tables["total"].Rows[0];
Application[ "SessionCount" ]=System.Convert.ToInt32(drowTitle["totals"].ToString().Trim());
}
}
void Session_End() {
Application["SessionCount"] = 0;
}
</script>
SessionCount.aspx里的代码
复制代码 代码如下:
void Page_Load(Object sender , EventArgs e)
{
int total = 0;
string strSelect;
SqlConnection conPubs;
//要执行某项数据操作要用SqlCommand方式调用
SqlCommand cmdSql;
//为了防止同文档里的其他页面在访问时也进行累加运算
int intHits = 0;
intHits = (int)Application["SessionCount"];
intHits += 1;
Application["SessionCount"] = intHits;
lblSessionCount.Text = Application[ "SessionCount" ].ToString();
total = (int)Application["SessionCount"];
strSelect = "update total set totals= @total";
conPubs = new SqlConnection(@"Server=localhost;Integrated Security=SSPI;Database=test");
cmdSql = new SqlCommand(strSelect, conPubs);
cmdSql.Parameters.Add("@total", total);
conPubs.Open();
cmdSql.ExecuteNonQuery();
conPubs.Close();
}
上段代码有个小问题,就是过了一段时间后,Application["SessionCount"]的值会变成0,而且由于前面设置了一个初始的0,也会连带的把数据库里原来保存的值更新为0起始.
更改后
global.asax
复制代码 代码如下:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
string strSelect;
SqlConnection conPubs;
SqlDataAdapter dadPubs;
DataSet dstTitles;
DataRow drowTitle;
void Session_Start(Object sender , EventArgs e)
{
if ( Application[ "SessionCount" ] == null ) {
Application[ "SessionCount" ] = 0;
strSelect = "SELECT totals From total";
conPubs = new SqlConnection(@"Server=localhost;Integrated Security=SSPI;Database=test");
dadPubs = new SqlDataAdapter(strSelect, conPubs);
dstTitles = new DataSet();
dadPubs.Fill(dstTitles, "total");
drowTitle = dstTitles.Tables["total"].Rows[0];
Application[ "SessionCount" ]=System.Convert.ToInt32(drowTitle["totals"].ToString().Trim());
}
}
void Session_End() {
Application["SessionCount"] = null;
}
</script>
SessionCount.aspx
复制代码 代码如下:
<script language="C#" runat="server">
void Page_Load(Object sender , EventArgs e)
{
int total = 0;
string strSelect;
SqlConnection conPubs;
//要执行某项数据操作要用SqlCommand方式调用
SqlCommand cmdSql;
//为了防止同文档里的其他页面在访问时也进行累加运算
int intHits = 0;
intHits = (int)Application["SessionCount"];
intHits += 1;
total = intHits;
lblSessionCount.Text = intHits.ToString();
strSelect = "update total set totals= @total";
conPubs = new SqlConnection(@"Server=localhost;Integrated Security=SSPI;Database=test");
cmdSql = new SqlCommand(strSelect, conPubs);
cmdSql.Parameters.Add("@total", total);
conPubs.Open();
cmdSql.ExecuteNonQuery();
conPubs.Close();
Application["SessionCount"] = null;
}
</script>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Servlet实现统计页面访问次数的具体代码,供大家参考,具体内容如下实现思路:1.新建一个CallServlet类继承HttpServle
本文实例讲述了JS+JSP通过img标签调用实现静态页面访问次数统计的方法。分享给大家供大家参考,具体如下:测试页面:test.htmltestthisisat
单个select语句实现MySQL查询统计次数单个select语句实现MySQL查询统计次数的方法用处在哪里呢?用处太多了,比如一个成绩单,你要查询及格得人数与
本文实例讲述了jsp利用application统计在线人数的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:application简单的页面访问
本文实例讲述了PHP实现限制IP访问及提交次数的方法。分享给大家供大家参考,具体如下:一、原理提交次数是肯定要往数据库里写次数这个数据的,比如用户登陆,当用户出