国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 达孜县| 东方市| 遵义市| 化隆| 开化县| 突泉县| 玉树县| 奉贤区| 南郑县| 扶风县| 肃北| 阳城县| 板桥市| 出国| 九龙坡区| 同仁县| 武功县| 平顶山市| 桂平市| 滕州市| 社会| 南川市| 成安县| 山阳县| 湖南省| 会理县| 金秀| 金湖县| 洪湖市| 海口市| 葵青区| 和顺县| 喀什市| 泸州市| 禹城市| 忻州市| 信阳市| 灵丘县| 英超| 兴安盟| 乡宁县|