服务器之家

服务器之家 > 正文

.net页面访问次数统计实现原理与代码

时间:2019-10-17 10:40     来源/作者:asp.net代码网

数据库准备:建立一个表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>
标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部