.net页面访问次数统计实现原理与代码
数据库准备:建立一个表total里面数据项为totals类型为varchar 50
.语言环境: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>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程