国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.net頁面訪問次數(shù)統(tǒng)計(jì)實(shí)現(xiàn)原理與代碼

2024-07-10 13:18:09
字體:
供稿:網(wǎng)友

數(shù)據(jù)庫準(zhǔn)備:建立一個表total里面數(shù)據(jù)項(xiàng)為totals類型為varchar 50
.net語言環(huán)境:C#
global.asax里的代碼

復(fù)制代碼 代碼如下:


<%@ 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里的代碼

復(fù)制代碼 代碼如下:


void Page_Load(Object sender , EventArgs e)
{
    int total = 0;
    string strSelect;
    SqlConnection conPubs;
    //要執(zhí)行某項(xiàng)數(shù)據(jù)操作要用SqlCommand方式調(diào)用
    SqlCommand cmdSql;
    //為了防止同文檔里的其他頁面在訪問時也進(jìn)行累加運(yùn)算
    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,而且由于前面設(shè)置了一個初始的0,也會連帶的把數(shù)據(jù)庫里原來保存的值更新為0起始.
更改后
global.asax

復(fù)制代碼 代碼如下:


<%@ 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

復(fù)制代碼 代碼如下:


<script language="C#" runat="server">
void Page_Load(Object sender , EventArgs e)
{
    int total = 0;
    string strSelect;
    SqlConnection conPubs;
    //要執(zhí)行某項(xiàng)數(shù)據(jù)操作要用SqlCommand方式調(diào)用
    SqlCommand cmdSql;
    //為了防止同文檔里的其他頁面在訪問時也進(jìn)行累加運(yùn)算
    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>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东乡族自治县| 黄冈市| 怀仁县| 威信县| 罗江县| 英山县| 武威市| 浠水县| 宣汉县| 斗六市| 乌兰察布市| 长丰县| 莎车县| 闵行区| 盘锦市| 柳林县| 天峨县| 巴东县| 宁都县| 阿勒泰市| 陇南市| 夏津县| 青铜峡市| 望城县| 深圳市| 深水埗区| 威宁| 邢台市| 通化市| 梧州市| 湖北省| 隆安县| 新宁县| 松阳县| 白水县| 西贡区| 读书| 公主岭市| 安岳县| 安岳县| 茶陵县|