不计入网站访问者并将其存储在数据库中?

时间:2014-04-29 06:25:20

标签: asp.net

我想要一个关于如何计算ASP.NET Web应用程序的网站访问者数量的解决方案。然后,我想了解如何将其存储在数据库中。

哪些资源,教程等可以帮助我开始这个?

1 个答案:

答案 0 :(得分:1)

您可以使用Global.asax文件

来完成 global.asax 文件

中的

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfVisitors"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}

aspx页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>No of Visits:</b>
</td>
<td>
<asp:Label ID="lblCount" runat="server" ForeColor="Red" />
</td>
</tr>
</table>
</form>
</body>
</html>

<强> aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
 if(!IsPostBack)
  { 
    lblCount.Text = Application["NoOfVisitors"].ToString();
    var cnnString =         ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    var cmd = "insert into Table values(@Count)";
    using (SqlConnection cnn = new SqlConnection(cnnString))
    {
        using (SqlCommand cmd = new SqlCommand(cmd, cnn))
        {
            cmd.Parameters.AddWithValue("@Count",lblCount.Text);

            cnn.Open();
            cmd.ExecuteNonQuery();
        }
    }

   }
}