欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-04 15:49:23
数据库准备:建立一个表total里面数据项为totals类型为varchar 50.net语言环境:c#global.asax里的代码复制代码 代码如下:<%@ im...

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