. NET in Global.asax easy to implement site access statistics __.net

Source: Internet
Author: User
Tags connectionstrings
The specific principles are:
At the beginning of the application, read the numbers from a file and put them in a application.
This is mainly to prevent the site from restarting when the zero clean
And then there's session_start, variable +1,
Update the data again at application_end time,
The code is as follows:

<%@ application Language= "C #"%>
<%@ Import Namespace = "System.Data.SqlClient"%>

<script runat= "Server" >

void Application_Start (object sender, EventArgs e)
{
Code that runs when the application starts
SqlConnection con = new SqlConnection ();
Con. ConnectionString = configurationmanager.connectionstrings["tongjiconnectionstring"]. ConnectionString;
Con. Open ();
SqlCommand cmd = new SqlCommand ("SELECT * from Liulantongji", con);
int count = Convert.ToInt32 (cmd. ExecuteScalar ());
Con. Close ();
application["Total" = count;

}

void Application_End (object sender, EventArgs e)
{
Code that runs when the application shuts down
SqlConnection con = new SqlConnection ();
Con. ConnectionString = configurationmanager.connectionstrings["tongjiconnectionstring"]. ConnectionString;
Con. Open ();
SqlCommand cmd = new SqlCommand ("update Liulantongji set totalnum =" + application["Total"]. ToString (), con);
Cmd. ExecuteNonQuery ();
Con. Close ();

}

void Application_Error (object sender, EventArgs e)
{
Code that runs when an unhandled error occurs

}

void Session_Start (object sender, EventArgs e)
{
session["admin" = null;
session["Admintype"] = null;
Code that runs when a new session starts
Application.Lock ();
application["Total"] =convert.toint32 (application["Total") + 1;
Application.UnLock ();

}

void Session_End (object sender, EventArgs e)
{
Code that runs at the end of the session.
Note: Only the sessionstate mode in the Web.config file is set to
The Session_End event is raised when InProc. If the session mode is set to StateServer
or SQL Server, the event is not raised.



}

</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.