Implementation principle and code of. net page access count statistics

Source: Internet
Author: User

Database preparation: Create a table with data items of the totals type varchar 50 in total
. Net Language Environment: C #
Code in global. asax
Copy codeThe Code is as follows:
<% @ 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>

Code in SessionCount. aspx
Copy codeThe Code is as follows:
Void Page_Load (Object sender, EventArgs e)
{
Int total = 0;
String strSelect;
SqlConnection conPubs;
// To perform a data operation, use SqlCommand.
SqlCommand explain SQL;
// To prevent other pages in the same document from accumulating operations during access
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 ");
Explain SQL = new SqlCommand (strSelect, conPubs );
Using SQL. Parameters. Add ("@ total", total );
ConPubs. Open ();
Explain SQL. ExecuteNonQuery ();
ConPubs. Close ();
}

There is a small problem in the code above, that is, after a period of time, the value of Application ["SessionCount"] will change to 0, and because an initial 0 is set, it also updates the previously saved value in the database to 0.
After modification
Global. asax
Copy codeThe Code is as follows:
<% @ 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
Copy codeThe Code is as follows:
<Script language = "C #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
Int total = 0;
String strSelect;
SqlConnection conPubs;
// To perform a data operation, use SqlCommand.
SqlCommand explain SQL;
// To prevent other pages in the same document from accumulating operations during access
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 ");
Explain SQL = new SqlCommand (strSelect, conPubs );
Using SQL. Parameters. Add ("@ total", total );
ConPubs. Open ();
Explain SQL. ExecuteNonQuery ();
ConPubs. Close ();
Application ["SessionCount"] = null;
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.