L from http://hi.baidu.com/mydelicious/blog/item/22b10d38cb14ef25b8998fbb.html
Preparation 1. Create a Tongji database and add a Tongji table. The table contains a number field, which is of the int type and the initial numger value is 1000. 2. Create a website; 3. Create a database connection string (<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 is run when an unprocessed error occurs}
Void session_start (Object sender, eventargs E)
{// The code application. Lock () that runs when a new session is started ();
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.
Note 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, it is displayed that the total number of visitors is 1002; the current number of online users is 2; this indicates that 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. However, this time I was given an unexpected gift. In vs2005, select "save global" from the "file" menu. In this case, 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.