欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年01月17日 08:55:33   作者:  
網(wǎng)站訪問量統(tǒng)計、頁面訪問次數(shù)統(tǒng)計,比較實用的一個功能,很多新手朋友都想實現(xiàn),本文處于此目的整理了一些,感興趣的朋友可以了解下

數(shù)據(jù)庫準(zhǔn)備:建立一個表total里面數(shù)據(jù)項為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í)行某項數(shù)據(jù)操作要用SqlCommand方式調(diào)用
    SqlCommand cmdSql;
    //為了防止同文檔里的其他頁面在訪問時也進(jì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í)行某項數(shù)據(jù)操作要用SqlCommand方式調(diào)用
    SqlCommand cmdSql;
    //為了防止同文檔里的其他頁面在訪問時也進(jì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>

相關(guān)文章

最新評論