.net頁(yè)面訪問(wèn)次數(shù)統(tǒng)計(jì)實(shí)現(xiàn)原理與代碼
數(shù)據(jù)庫(kù)準(zhǔn)備:建立一個(gè)表total里面數(shù)據(jù)項(xiàng)為totals類(lèi)型為varchar 50
.net語(yǔ)言環(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í)行某項(xiàng)數(shù)據(jù)操作要用SqlCommand方式調(diào)用
SqlCommand cmdSql;
//為了防止同文檔里的其他頁(yè)面在訪問(wèn)時(shí)也進(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();
}
上段代碼有個(gè)小問(wèn)題,就是過(guò)了一段時(shí)間后,Application["SessionCount"]的值會(huì)變成0,而且由于前面設(shè)置了一個(gè)初始的0,也會(huì)連帶的把數(shù)據(jù)庫(kù)里原來(lái)保存的值更新為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í)行某項(xiàng)數(shù)據(jù)操作要用SqlCommand方式調(diào)用
SqlCommand cmdSql;
//為了防止同文檔里的其他頁(yè)面在訪問(wèn)時(shí)也進(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>
- 獲取php頁(yè)面執(zhí)行時(shí)間,數(shù)據(jù)庫(kù)讀寫(xiě)次數(shù),函數(shù)調(diào)用次數(shù)等(THINKphp)
- java字符串比較獲取字符串出現(xiàn)次數(shù)的示例
- c#字符串查找某詞出現(xiàn)的次數(shù)及索引
- Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法
- php計(jì)算數(shù)組相同值出現(xiàn)次數(shù)的代碼(array_count_values)
- JS實(shí)現(xiàn)在線統(tǒng)計(jì)一個(gè)頁(yè)面內(nèi)鼠標(biāo)點(diǎn)擊次數(shù)的方法
- python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法
- java發(fā)送短信系列之限制日發(fā)送次數(shù)
- C++計(jì)算每個(gè)字符出現(xiàn)的次數(shù)
- C# winform實(shí)現(xiàn)登陸次數(shù)限制
相關(guān)文章
.Net Core配置Configuration具體實(shí)現(xiàn)
這篇文章主要介紹了.Net Core配置Configuration具體實(shí)現(xiàn),文中運(yùn)用大量代碼進(jìn)行講解,如果有對(duì)相關(guān)知識(shí)感興趣的小伙伴可以參考這篇文章,希望可以幫助到你2021-09-09ASP.NET的適配器設(shè)計(jì)模式(Adapter)應(yīng)用詳解
有關(guān)設(shè)計(jì)模式的適配器模式(Adapter)確實(shí)不是很好理解理解,接下來(lái)將做一個(gè)簡(jiǎn)單的例子簡(jiǎn)要說(shuō)明下,感興趣的朋友可不要錯(cuò)過(guò)了哈,希望本文可以幫助到你更好的理解適配器設(shè)計(jì)模式2013-02-02.NET6打包部署到Windows?Service的全過(guò)程
net用了這么久,雖然多數(shù)都是部署在centos系統(tǒng),但也有部署在windows上的情況,下面這篇文章主要給大家介紹了關(guān)于.NET6打包部署到Windows?Service的相關(guān)資料,需要的朋友可以參考下2022-10-10在Framework4.0中實(shí)現(xiàn)延遲加載的實(shí)現(xiàn)方法
延遲加載,亦稱(chēng)延遲實(shí)例化,延遲初始化等,主要表達(dá)的思想是,把對(duì)象的創(chuàng)建將會(huì)延遲到使用時(shí)創(chuàng)建,而不是在對(duì)象實(shí)例化時(shí)創(chuàng)建對(duì)象,即用時(shí)才加載。2011-08-08關(guān)于Asp.net頁(yè)面Page_Load被執(zhí)行兩次的問(wèn)題分享
這篇文章介紹了關(guān)于Asp.net頁(yè)面Page_Load被執(zhí)行兩次的問(wèn)題,有需要的朋友可以參考一下2013-09-09利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法
這篇文章介紹了利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法,有需要的朋友可以參考一下,希望對(duì)你有所幫助2013-07-07