Asp.net website traffic and simultaneous visitor count

Source: Internet
Author: User

Create a global. asax (global application class)

C # code
<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
//
System. Data. sqlclient. sqlconnection con = dbconnect. createconnection ();
Con. open ();
System. Data. sqlclient. sqlcommand COM = new system. Data. sqlclient. sqlcommand (
"Select * from total", con );
Int COUNT = system. Convert. toint32 (COM. executescalar ());
Con. Close ();

Application ["Total"] = count;
Application ["online"] = 0;

}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed
System. Data. sqlclient. sqlconnection con = dbconnect. createconnection ();
Con. open ();
System. Data. sqlclient. sqlcommand COM = new system. Data. sqlclient. sqlcommand (
"Update total set num =" + application ["Total"]. tostring (), con );
Com. 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)
{
// The code that runs when the new session starts
Session. Timeout = 1;
Application. Lock ();
Application ["Total"] = system. Convert. toint32 (application ["Total"]) + 1;
Application ["online"] = system. Convert. toint32 (application ["online"]) + 1;
Application. Unlock ();
}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.
Application. Lock ();
Application ["online"] = system. Convert. toint32 (application ["online"])-1;
Application. Unlock ();
}

</SCRIPT>

 

 

Read:

Labtotal. Text = application ["Total"]. tostring ();

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.