c#

位置:IT落伍者 >> c# >> 浏览文章

.net实现页面访问次数统计


发布日期:2021年10月06日
 
.net实现页面访问次数统计

数据库准备:建立一个表total里面数据项为totals类型为varchar

语言环境:C#

globalasax里的代码

<%@ Import Namespace=SystemData %>

<%@ Import Namespace=SystemDataSqlClient %>

<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 ] = ;

strSelect = SELECT totals From total;

conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

dadPubs = new SqlDataAdapter(strSelect conPubs);

dstTitles = new DataSet();

dadPubsFill(dstTitles total);

drowTitle = dstTitlesTables[total]Rows[];

Application[ SessionCount ]=SystemConvertToInt(drowTitle[totals]ToString()Trim());

}

}

void Session_End() {

Application[SessionCount] = ;

}

</script>

============================

SessionCountaspx里的代码

void Page_Load(Object sender EventArgs e)

{

int total = ;

string strSelect;

SqlConnection conPubs;

//要执行某项数据操作要用SqlCommand方式调用

SqlCommand cmdSql;

//为了防止同文档里的其他页面在访问时也进行累加运算

int intHits = ;

intHits = (int)Application[SessionCount];

intHits += ;

Application[SessionCount] = intHits;

lblSessionCountText = 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);

cmdSqlParametersAdd(@total total);

conPubsOpen();

cmdSqlExecuteNonQuery();

conPubsClose();

}

================上段代码有个小问题就是过了一段时间后Application[SessionCount]的值会变成而且由于前面设置了一个初始的也会连带的把数据库里原来保存的值更新为起始

更改后

globalasax

<%@ Import Namespace=SystemData %>

<%@ Import Namespace=SystemDataSqlClient %>

<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 ] = ;

strSelect = SELECT totals From total;

conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

dadPubs = new SqlDataAdapter(strSelect conPubs);

dstTitles = new DataSet();

dadPubsFill(dstTitles total);

drowTitle = dstTitlesTables[total]Rows[];

Application[ SessionCount ]=SystemConvertToInt(drowTitle[totals]ToString()Trim());

}

}

void Session_End() {

Application[SessionCount] = null;

}

</script>

SessionCountaspx

<script language=C# runat=server>

void Page_Load(Object sender EventArgs e)

{

int total = ;

string strSelect;

SqlConnection conPubs;

//要执行某项数据操作要用SqlCommand方式调用

SqlCommand cmdSql;

//为了防止同文档里的其他页面在访问时也进行累加运算

int intHits = ;

intHits = (int)Application[SessionCount];

intHits += ;

total = intHits;

lblSessionCountText = intHitsToString();

strSelect = update total set totals= @total;

conPubs = new SqlConnection(@Server=localhost;Integrated Security=SSPI;Database=test);

cmdSql = new SqlCommand(strSelect conPubs);

cmdSqlParametersAdd(@total total);

conPubsOpen();

cmdSqlExecuteNonQuery();

conPubsClose();

Application[SessionCount] = null;

}

</script>

               

上一篇:C#编程入门基础 控制语句概要

下一篇:Visual Basic.NET中使用ADO访问数据库