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

首頁 > 編程 > .NET > 正文

.net頁面訪問次數統計實現原理與代碼

2020-01-18 00:18:02
字體:
來源:轉載
供稿:網友

數據庫準備:建立一個表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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大关县| 阜南县| 福安市| 河间市| 荥经县| 彭山县| 宁强县| 祁阳县| 娄底市| 永善县| 扎兰屯市| 勐海县| 盐山县| 六安市| 普陀区| 桦川县| 开封县| 那坡县| 许昌县| 潜山县| 阜宁县| 甘洛县| 菏泽市| 秦安县| 武夷山市| 宕昌县| 焦作市| 济阳县| 班玛县| 门源| 内黄县| 永胜县| 忻州市| 乌拉特中旗| 马公市| 社会| 十堰市| 洪江市| 富平县| 文登市| 三门峡市|