How to count the number of online users and the number of historical visitors

Source: Internet
Author: User
Tags connectionstrings

Preparations
1. Create a database for Tongji and add a table for Tongji. There is a number field in the table, which is of the int type. The initial value of number is 1000;
2. Create a website;
3. Create a database connection string (specific method) and save it to the Web. config file. The code in the <connectionstrings> section is as follows:

<Connectionstrings>
<Add name = "tongjiconnectionstring" connectionstring = "Data Source =.; initial catalog = Tongji; Integrated Security = true" providername = "system. Data. sqlclient"/>
</Connectionstrings>


Key code
4. Add a new project/global application class: Global. asax. The code for the file 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 Tongji", con );
Int COUNT = convert. toint32 (CMD. executescalar ());
Con. Close ();
Application ["Total"] = count;
Application ["online"] = 0;
}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed
Sqlconnection con = new sqlconnection ();
Con. connectionstring = configurationmanager. connectionstrings ["tongjiconnectionstring"]. connectionstring;
Con. open ();
Sqlcommand cmd = new sqlcommand ("Update Tongji set number =" + 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)
{
// The code that runs when the new session starts
Application. Lock ();
Application ["Total"] = (INT) application ["Total"] + 1;
Application ["online"] = (INT) application ["online"] + 1;
Application. Unlock ();
}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
Application. Lock ();
Application ["online"] = (INT) application ["online"]-1;
Application. Unlock ();
}

</SCRIPT>


Run the test
5. Drag two labels to default. aspx;
6. Its default. aspx. CS code is as follows:

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
This. label1.text = "Total Number of visitors" + application ["Total"]. tostring ();
This. label2.text = "Current online quantity" + application ["online"]. tostring ();
}
}

7. OK !! Start debugging.

Notes

8. When debugging in vs2005, the total number of visitors is 1001. Currently, the number of online users is 1;
I re-open another IE and copy the address to it. The total number of visitors is 1002, and the current number of online users is 2;
This means everything is normal. But it is still 1000 in the database ,??????
I am here to delay a lot of time (one day), but in my despair, I tried again,
This time, however, God gave me an unexpected gift.

In vs2005, select "save global" from the "file" menu ";
In this case, the value 1000 in the database is updated to 1002.
If the same is true for I s debugging, you must shut down or stop the WWW Service before writing data into the database.

Knowledge Summary

· Database connection and use of related objects
· Application object usage
· Use of session objects

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.