. 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 # global. code in 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. code void Page_Load (Object sender, EventArgs e) in aspx {int total = 0; string strSelect; SqlConnection conPubs; // to execute a data operation, use SqlCommand to call SqlCommand prepare SQL; // to prevent other pages in the same document from accumulating 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); explain SQL. parameters. add ("@ total", total); conPubs. open (); explain SQL. executeNonQuery (); conPubs. close () ;}====================== there is a small problem with the code above, that is, after a while, the value of Application ["SessionCount"] is changed to 0, and because an initial 0 is set, the previously saved value in the database is updated to 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; // to execute a data operation, use SqlCommand to call SqlCommand prepare SQL; // to prevent other pages in the same document from being accessed, the cumulative operation 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); explain SQL. parameters. add ("@ total", total); conPubs. open (); explain SQL. executeNonQuery (); conPubs. close (); Application ["SessionCount"] = null ;}</script>

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.