The number of online people and the number of historical visitors to the site

Source: Internet
Author: User
Tags connectionstrings

L'm from Http://hi.baidu.com/mydelicious/blog/item/22b10d38cb14ef25b8998fbb.html.

Prepare for Work 1, create a new Tongji database, add a Tongji table with a number field in the table, an int type, Numger initial value of 1000; 2, create a new Web site, 3, create a new database connection string (< connectionstrings> <add name= "tongjiconnectionstring" connectionstring= "Data source=.;i Nitial catalog=tongji;integrated security=true "providername= System.Data.SqlClient"/> </connectionstrings >

Key code 4, add New Item/Global Application class: Global.asax, the full code of its 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 to run when the application shuts down

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 to run when an unhandled error occurs}

void Session_Start (object sender, EventArgs e)

{//Code Application.Lock () to run when a new session starts;

application["Total"] = (int) application["Total"] + 1;

application["Online"] = (int) application["online" + 1;

Application.UnLock (); }

void Session_End (object sender, EventArgs e)

{//code that runs at the end of the session.

Application.Lock ();

application["Online"] = (int) application["Online"]-1;

Application.UnLock (); }

</script>

Run Test 5,

Drag two lable to Default.aspx;

6, its Default.ASPx.cs code 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["sum"]. ToString ();

This. Label2.Text = "Current online number" + application["on-line"]. ToString ();

}

}

7, OK. Start Debugging.

Note 8, I in the VS2005 debugging, show: Total number of visitors to 1001; current online number of 1 when;       I reopened another IE, and the address copy past, then show: Total number of visitors to 1002; current online number 2; This shows that everything is fine.       However, the database is still 1000,。。。。。。       I was here to delay a lot of time (a day), but in my despair, I tried more than once, however, this time Providence gave me unexpected gifts.       At this point, I am in VS2005, "File" menu, choose "Save Global", at this time, 1000 of the database is not updated to 1002. If debugging is the same in I I S, the data is written to the database only if you shut down or stop the WWW service normally.

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.