數據庫准備:建立一個表total裡面數據項為totals類型為varchar 50
.net語言環境:C#
global.asax裡的代碼
復制代碼 代碼如下:
<%@ 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>
SessionCount.aspx裡的代碼
復制代碼 代碼如下:
void Page_Load(Object sender , EventArgs e)
{
int total = 0;
string strSelect;
SqlConnection conPubs;
//要執行某項數據操作要用SqlCommand方式調用
SqlCommand cmdSql;
//為了防止同文檔裡的其他頁面在訪問時也進行累加運算
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");
cmdSql = new SqlCommand(strSelect, conPubs);
cmdSql.Parameters.Add("@total", total);
conPubs.Open();
cmdSql.ExecuteNonQuery();
conPubs.Close();
}
上段代碼有個小問題,就是過了一段時間後,Application["SessionCount"]的值會變成0,而且由於前面設置了一個初始的0,也會連帶的把數據庫裡原來保存的值更新為0起始.
更改後
global.asax
復制代碼 代碼如下:
<%@ 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
復制代碼 代碼如下:
<script language="C#" runat="server">
void Page_Load(Object sender , EventArgs e)
{
int total = 0;
string strSelect;
SqlConnection conPubs;
//要執行某項數據操作要用SqlCommand方式調用
SqlCommand cmdSql;
//為了防止同文檔裡的其他頁面在訪問時也進行累加運算
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");
cmdSql = new SqlCommand(strSelect, conPubs);
cmdSql.Parameters.Add("@total", total);
conPubs.Open();
cmdSql.ExecuteNonQuery();
conPubs.Close();
Application["SessionCount"] = null;
}
</script>