数据库准备:建立一个表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>

您可能感兴趣的文章:

  • 获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
  • java字符串比较获取字符串出现次数的示例
  • c#字符串查找某词出现的次数及索引
  • Python统计列表中的重复项出现的次数的方法
  • php计算数组相同值出现次数的代码(array_count_values)
  • JS实现在线统计一个页面内鼠标点击次数的方法
  • python统计字符串中指定字符出现次数的方法
  • java发送短信系列之限制日发送次数
  • C++计算每个字符出现的次数
  • C# winform实现登陆次数限制
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。