.net頁面訪問次數(shù)統(tǒng)計實現(xiàn)原理與代碼
數(shù)據(jù)庫準(zhǔn)備:建立一個表total里面數(shù)據(jù)項為totals類型為varchar 50
.net語言環(huán)境: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;
//要執(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
<%@ 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;
//要執(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>
- 獲取php頁面執(zhí)行時間,數(shù)據(jù)庫讀寫次數(shù),函數(shù)調(diào)用次數(shù)等(THINKphp)
- java字符串比較獲取字符串出現(xiàn)次數(shù)的示例
- c#字符串查找某詞出現(xiàn)的次數(shù)及索引
- Python統(tǒng)計列表中的重復(fù)項出現(xiàn)的次數(shù)的方法
- php計算數(shù)組相同值出現(xiàn)次數(shù)的代碼(array_count_values)
- JS實現(xiàn)在線統(tǒng)計一個頁面內(nèi)鼠標(biāo)點擊次數(shù)的方法
- python統(tǒng)計字符串中指定字符出現(xiàn)次數(shù)的方法
- java發(fā)送短信系列之限制日發(fā)送次數(shù)
- C++計算每個字符出現(xiàn)的次數(shù)
- C# winform實現(xiàn)登陸次數(shù)限制
相關(guān)文章
.Net Core配置Configuration具體實現(xiàn)
這篇文章主要介紹了.Net Core配置Configuration具體實現(xiàn),文中運用大量代碼進(jìn)行講解,如果有對相關(guān)知識感興趣的小伙伴可以參考這篇文章,希望可以幫助到你2021-09-09ASP.NET的適配器設(shè)計模式(Adapter)應(yīng)用詳解
有關(guān)設(shè)計模式的適配器模式(Adapter)確實不是很好理解理解,接下來將做一個簡單的例子簡要說明下,感興趣的朋友可不要錯過了哈,希望本文可以幫助到你更好的理解適配器設(shè)計模式2013-02-02在Framework4.0中實現(xiàn)延遲加載的實現(xiàn)方法
延遲加載,亦稱延遲實例化,延遲初始化等,主要表達(dá)的思想是,把對象的創(chuàng)建將會延遲到使用時創(chuàng)建,而不是在對象實例化時創(chuàng)建對象,即用時才加載。2011-08-08關(guān)于Asp.net頁面Page_Load被執(zhí)行兩次的問題分享
這篇文章介紹了關(guān)于Asp.net頁面Page_Load被執(zhí)行兩次的問題,有需要的朋友可以參考一下2013-09-09利用.net控件實現(xiàn)下拉導(dǎo)航菜單制作的具體方法
這篇文章介紹了利用.net控件實現(xiàn)下拉導(dǎo)航菜單制作的具體方法,有需要的朋友可以參考一下,希望對你有所幫助2013-07-07