Database preparation: Create a table with data items of the totals type varchar 50 in total
. Net Language Environment: C #
Code in global. asax
Copy codeThe Code is as follows:
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
<Script language = "C #" runat = "server">
String strSelect;
SqlConnection conPubs;
SqlDataAdapter dadPubs;
DataSet dstTitles;
DataRow drowTitle;
Void Session_Start (Object sender, EventArgs e)
{
If (Application ["SessionCount"] = null ){
Application ["SessionCount"] = 0;
StrSelect = "SELECT totals From total ";
ConPubs = new SqlConnection (@ "Server = localhost; Integrated Security = SSPI; Database = test ");
DadPubs = new SqlDataAdapter (strSelect, conPubs );
DstTitles = new DataSet ();
DadPubs. Fill (dstTitles, "total ");
DrowTitle = dstTitles. Tables ["total"]. Rows [0];
Application ["SessionCount"] = System. Convert. ToInt32 (drowTitle ["totals"]. ToString (). Trim ());
}
}
Void Session_End (){
Application ["SessionCount"] = 0;
}
</Script>
Code in SessionCount. aspx
Copy codeThe Code is as follows:
Void Page_Load (Object sender, EventArgs e)
{
Int total = 0;
String strSelect;
SqlConnection conPubs;
// To perform a data operation, use SqlCommand.
SqlCommand explain SQL;
// To prevent other pages in the same document from accumulating operations during access
Int intHits = 0;
IntHits = (int) Application ["SessionCount"];
IntHits + = 1;
Application ["SessionCount"] = intHits;
LblSessionCount. Text = Application ["SessionCount"]. ToString ();
Total = (int) Application ["SessionCount"];
StrSelect = "update total set totals = @ total ";
ConPubs = new SqlConnection (@ "Server = localhost; Integrated Security = SSPI; Database = test ");
Explain SQL = new SqlCommand (strSelect, conPubs );
Using SQL. Parameters. Add ("@ total", total );
ConPubs. Open ();
Explain SQL. ExecuteNonQuery ();
ConPubs. Close ();
}
There is a small problem in the code above, that is, after a period of time, the value of Application ["SessionCount"] will change to 0, and because an initial 0 is set, it also updates the previously saved value in the database to 0.
After modification
Global. asax
Copy codeThe Code is as follows:
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
<Script language = "C #" runat = "server">
String strSelect;
SqlConnection conPubs;
SqlDataAdapter dadPubs;
DataSet dstTitles;
DataRow drowTitle;
Void Session_Start (Object sender, EventArgs e)
{
If (Application ["SessionCount"] = null ){
Application ["SessionCount"] = 0;
StrSelect = "SELECT totals From total ";
ConPubs = new SqlConnection (@ "Server = localhost; Integrated Security = SSPI; Database = test ");
DadPubs = new SqlDataAdapter (strSelect, conPubs );
DstTitles = new DataSet ();
DadPubs. Fill (dstTitles, "total ");
DrowTitle = dstTitles. Tables ["total"]. Rows [0];
Application ["SessionCount"] = System. Convert. ToInt32 (drowTitle ["totals"]. ToString (). Trim ());
}
}
Void Session_End (){
Application ["SessionCount"] = null;
}
</Script>
SessionCount. aspx
Copy codeThe Code is as follows:
<Script language = "C #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
Int total = 0;
String strSelect;
SqlConnection conPubs;
// To perform a data operation, use SqlCommand.
SqlCommand explain SQL;
// To prevent other pages in the same document from accumulating operations during access
Int intHits = 0;
IntHits = (int) Application ["SessionCount"];
IntHits + = 1;
Total = intHits;
LblSessionCount. Text = intHits. ToString ();
StrSelect = "update total set totals = @ total ";
ConPubs = new SqlConnection (@ "Server = localhost; Integrated Security = SSPI; Database = test ");
Explain SQL = new SqlCommand (strSelect, conPubs );
Using SQL. Parameters. Add ("@ total", total );
ConPubs. Open ();
Explain SQL. ExecuteNonQuery ();
ConPubs. Close ();
Application ["SessionCount"] = null;
}
</Script>