
 CREATE TABLE [dbo].[Yp_VisitStat] (
CREATE TABLE [dbo].[Yp_VisitStat] ( [iVisitStat_ID] [int] IDENTITY (1, 1) NOT NULL ,
    [iVisitStat_ID] [int] IDENTITY (1, 1) NOT NULL , [sIntoIP] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [sIntoIP] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [dIntoTime] [datetime] NULL
    [dIntoTime] [datetime] NULL  ) ON [PRIMARY]
) ON [PRIMARY] GO
GO
 ALTER TABLE [dbo].[Yp_VisitStat] ADD
ALTER TABLE [dbo].[Yp_VisitStat] ADD  CONSTRAINT [PK_Yp_VisitStat] PRIMARY KEY  CLUSTERED
    CONSTRAINT [PK_Yp_VisitStat] PRIMARY KEY  CLUSTERED  (
    ( [iVisitStat_ID]
        [iVisitStat_ID] )  ON [PRIMARY]
    )  ON [PRIMARY]  GO
GO <table class="tableBorder" cellspacing="1" cellpadding="2" width="100%" border="0" style="text-align: center">
<table class="tableBorder" cellspacing="1" cellpadding="2" width="100%" border="0" style="text-align: center"> <tr>
        <tr> <th style="HEIGHT: 21px" colspan="2" >
        <th style="HEIGHT: 21px" colspan="2" > <span style="font-size: 10pt; color: #ffffff">网站综合流量分析</span></th>
            <span style="font-size: 10pt; color: #ffffff">网站综合流量分析</span></th> </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">给合流量统计:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">给合流量统计:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>  
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>   <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
            <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> </td>
         </td> </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">上月流量统计:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">上月流量统计:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>  
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>   <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label> </td>
        </td>        </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">本月流量统计:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">本月流量统计:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>  
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>   <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label> </td>
        </td>        </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">昨日流量统计:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">昨日流量统计:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>  
        <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>   <asp:Label ID="Label10" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label10" runat="server" Text="Label"></asp:Label> </td>
        </td>        </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">今日流量统计:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">今日流量统计:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>  
        <asp:Label ID="Label11" runat="server" Text="Label"></asp:Label>   <asp:Label ID="Label12" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label12" runat="server" Text="Label"></asp:Label> </td>
        </td>        </tr>
        </tr> <tr>
        <tr> <td class="TableRow2" style="text-align: right; width: 126px;">当前在线状况:</td>
        <td class="TableRow2" style="text-align: right; width: 126px;">当前在线状况:</td> <td class="TableRow2" style="text-align: left;">
        <td class="TableRow2" style="text-align: left;"> <asp:Label ID="Label13" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label13" runat="server" Text="Label"></asp:Label> </td>
        </td>        </tr>
        </tr>         </table>
</table>   using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Collections;
using System.Collections; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls; using System.Data.SqlClient;
using System.Data.SqlClient;
 protected void Page_Load(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e) {
    { if (!Page.IsPostBack)
        if (!Page.IsPostBack) {
        {             VisitStat();
            VisitStat(); }
        } }
    } private void VisitStat()//流量分析
private void VisitStat()//流量分析 {
    { string LastMonthBegin = DateTime.Today.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:01";//上个月的起始时间
        string LastMonthBegin = DateTime.Today.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:01";//上个月的起始时间 string ThisMonthBegin = DateTime.Today.AddMonths(0).ToString("yyyy-MM") + "-01 00:00:01";//本个月的起始时间
        string ThisMonthBegin = DateTime.Today.AddMonths(0).ToString("yyyy-MM") + "-01 00:00:01";//本个月的起始时间 string NextMonthBegin = DateTime.Today.AddMonths(1).ToString("yyyy-MM") + "-01 00:00:01";//下个月的起始时间
        string NextMonthBegin = DateTime.Today.AddMonths(1).ToString("yyyy-MM") + "-01 00:00:01";//下个月的起始时间 string YesterdayBegin = DateTime.Now.AddDays(-1).ToString("d") + " 00:00:01";//昨天的起始时间
        string YesterdayBegin = DateTime.Now.AddDays(-1).ToString("d") + " 00:00:01";//昨天的起始时间 string TodayBegin = DateTime.Now.AddDays(0).ToString("d") + " 00:00:01";//今天的起始时间
        string TodayBegin = DateTime.Now.AddDays(0).ToString("d") + " 00:00:01";//今天的起始时间 string TomorrowBegin = DateTime.Now.AddDays(1).ToString("d") + " 00:00:01";//明天的起始时间
        string TomorrowBegin = DateTime.Now.AddDays(1).ToString("d") + " 00:00:01";//明天的起始时间 Label3.Text = "累计PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat]");
        Label3.Text = "累计PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat]"); Label4.Text = "累计独立IP浏览量:" + SqlResult("select distinct count(distinct sIntoIP) from [Yp_VisitStat]");
        Label4.Text = "累计独立IP浏览量:" + SqlResult("select distinct count(distinct sIntoIP) from [Yp_VisitStat]"); Label5.Text = "上个月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘");
        Label5.Text = "上个月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘"); Label6.Text = "上个月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘");
        Label6.Text = "上个月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + LastMonthBegin + "‘and dIntoTime<‘" + ThisMonthBegin + "‘"); Label7.Text = "本月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘");
        Label7.Text = "本月PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘"); Label8.Text = "本月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘");
        Label8.Text = "本月独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + ThisMonthBegin + "‘and dIntoTime<‘" + NextMonthBegin + "‘"); Label9.Text = "昨日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘");
        Label9.Text = "昨日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘"); Label10.Text = "昨日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘");
        Label10.Text = "昨日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + YesterdayBegin + "‘and dIntoTime<‘" + TodayBegin + "‘"); Label11.Text = "今日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘");
        Label11.Text = "今日PV浏览量:" + SqlResult("select count(sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘"); Label12.Text = "今日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘");
        Label12.Text = "今日独立IP浏览量:" + SqlResult("select count(distinct sIntoIP) from [Yp_VisitStat] where dIntoTime>‘" + TodayBegin + "‘and dIntoTime<‘" + TomorrowBegin + "‘"); Label13.Text = "当前共有:<b>" + Application["counter"].ToString() + "</b> 位访问者正在访问网站 !";
        Label13.Text = "当前共有:<b>" + Application["counter"].ToString() + "</b> 位访问者正在访问网站 !";         }
    }
 public static string SqlResult(string MySql)//查询库并返回记录个数
public static string SqlResult(string MySql)//查询库并返回记录个数 {
    { string strResult = null;
        string strResult = null; SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString());
        SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString()); Conn.Open();
        Conn.Open(); SqlCommand myCommand = new SqlCommand(MySql, Conn);
        SqlCommand myCommand = new SqlCommand(MySql, Conn); strResult = myCommand.ExecuteScalar().ToString();
        strResult = myCommand.ExecuteScalar().ToString(); Conn.Close();
        Conn.Close(); Conn.Dispose();
        Conn.Dispose(); return strResult;
        return strResult; }
    } <%@ Application Language="C#" %>
<%@ Application Language="C#" %> <%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlClient" %>

 <script runat="server">
<script runat="server">
 void Application_Start(object sender, EventArgs e)
    void Application_Start(object sender, EventArgs e)  {
    { // 在应用程序启动时运行的代码
        // 在应用程序启动时运行的代码  Application.Lock();
        Application.Lock();         Application["counter"] = 0;
        Application["counter"] = 0;         Application.UnLock();
        Application.UnLock(); 
         }
    } 
     void Application_End(object sender, EventArgs e)
    void Application_End(object sender, EventArgs e)  {
    { //  在应用程序关闭时运行的代码
        //  在应用程序关闭时运行的代码 Application.Lock();
        Application.Lock();         Application["counter"] = (int)Application["counter"] - 1;
        Application["counter"] = (int)Application["counter"] - 1;        Application.UnLock();
        Application.UnLock();
 }
    } 
         void Application_Error(object sender, EventArgs e)
    void Application_Error(object sender, EventArgs e)  {
    {  // 在出现未处理的错误时运行的代码
        // 在出现未处理的错误时运行的代码
 }
    }
 void Session_Start(object sender, EventArgs e)
    void Session_Start(object sender, EventArgs e)  {
    { // 在新会话启动时运行的代码
        // 在新会话启动时运行的代码 Application.Lock();
        Application.Lock(); Application["counter"] = (int)Application["counter"]+1;
        Application["counter"] = (int)Application["counter"]+1; string sIntoIP;
        string sIntoIP; string dIntoTime;
        string dIntoTime; if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) {
        { sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();            }
        } else
        else {
        { sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            sIntoIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();            }
        } dIntoTime = DateTime.Now.ToString();
        dIntoTime = DateTime.Now.ToString();        SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString());
        SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString()); Conn.Open();
        Conn.Open(); SqlCommand myCommand = new SqlCommand("insert into [Yp_VisitStat] (sIntoIP,dIntoTime) values (‘" + sIntoIP + "‘,‘" + dIntoTime + "‘)", Conn);
        SqlCommand myCommand = new SqlCommand("insert into [Yp_VisitStat] (sIntoIP,dIntoTime) values (‘" + sIntoIP + "‘,‘" + dIntoTime + "‘)", Conn); myCommand.ExecuteNonQuery();
        myCommand.ExecuteNonQuery(); Conn.Close();
        Conn.Close(); Conn.Dispose();
        Conn.Dispose(); Application.UnLock();
        Application.UnLock(); 
          }
    }
 void Session_End(object sender, EventArgs e)
    void Session_End(object sender, EventArgs e)  {
    { // 在会话结束时运行的代码。
        // 在会话结束时运行的代码。  // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer  // 或 SQLServer,则不会引发该事件。
        // 或 SQLServer,则不会引发该事件。            Application.Lock();
        Application.Lock();        Application["counter"] = (int)Application["counter"] - 1;
        Application["counter"] = (int)Application["counter"] - 1; Application.UnLock();
        Application.UnLock(); }
    } 
        </script>
</script>
Asp.net2.0下利用Global.asax全局文件实现流量分析
原文:http://www.cnblogs.com/yishuangyan/p/4357776.html