代码如下:
//========================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;using System.Xml;
using System.Data;namespace TestWeb 
{
/// <summary>
/// Global 篕璶磞瓃
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 砞璸ㄣ┮惠跑计
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
//Application["Count"] = 0;
Application.Lock();
//在线用户数,网站开始服务的时为0
Application["user_online"] = 0;  //取得计数器的当前值
XmlDocument dbConfig=new XmlDocument ();
string pathConfig=Server.MapPath("/lzlj_site/lzlj/bin/lzlj.config");//配置文件
dbConfig.Load(pathConfig);
Application["counter"]=int.Parse(dbConfig.SelectSingleNode("/configuration/homepage").Attributes["counter"].Value);   Application.UnLock (); 
}
 
protected void Session_Start(Object sender, EventArgs e)
{
/*int nCount = int.Parse(Application["Count"].ToString());
nCount ++;
Application["Count"] = Convert.ToString(nCount);
Console.WriteLine(Application["Count"].ToString());*/
Application.Lock();
//try
//{
//从dt_WebPage中读取所有页面,置入Session

//lzljLib.VisitTrace visitTrace=new lzljLib.VisitTrace();
//Session["VisitTrace"]=visitTrace.GetPageInfo(); Session["VisitTrace"]=this.GetPageInfo();
Session["VisitorIP"]=Request.UserHostAddress;
//在线用户+1

Application["user_online"] = (int)Application["user_online"] + 1;  //更新计数器+1
Application["counter"]=(int)Application["counter"]+1;
XmlDocument dbConfig=new XmlDocument ();
string pathConfig=Server.MapPath("/lzlj_site/lzlj/bin/lzlj.config");//配置文件
dbConfig.Load(pathConfig);
dbConfig.SelectSingleNode("/configuration/homepage").Attributes["counter"].Value=Application["counter"].ToString();
dbConfig.Save(pathConfig);

//}
// catch(Exception ex)
// {
// lzlj.admin.ShowMessage.Message="初始化来访信息时出错: "+ex.Message;
// Response.Redirect("/lzlj_site/lzlj/admin/ShowMessage.aspx");
// }
// finally
// {
Application.UnLock (); 
// }

} protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{
/*int nCount = int.Parse(Application["Count"].ToString());
nCount --;
Application["Count"] = Convert.ToString(nCount);
Console.WriteLine(Application["Count"].ToString());
*/
Application.Lock();
//保存来访信息
string userIP=Session["VisitorIP"].ToString();
Hashtable htuserInfo=(Hashtable)Session["VisitTrace"];

//在线用户-1  
Application["user_online"] = (int)Application["user_online"] - 1; 
Application.UnLock();
} protected void Application_End(Object sender, EventArgs e)
{
//更新计数器
Application.Lock();
XmlDocument dbConfig=new XmlDocument ();
string pathConfig=Server.MapPath("/lzlj_site/lzlj/bin/lzlj.config");//配置文件
dbConfig.Load(pathConfig);
dbConfig.SelectSingleNode("/configuration/homepage").Attributes["counter"].Value=Application["counter"].ToString();
dbConfig.Save(pathConfig);
Application.UnLock ();  }

#region Web Form 砞璸ㄣ玻ネ祘Α絏
/// <summary>
/// 砞璸ㄣや穿┮ゲ斗よ猭 - 叫づㄏノ祘Α絏絪胯竟э
/// 硂よ猭ず甧
/// </summary>
private void InitializeComponent()
{    
this.components = new System.ComponentModel.Container();
}
#endregion /// <summary>
/// 从dt_WebPage获取站内web页面信息,返回Hashtable,key与页号一一对应,路径,点击次数(初始化为0)存入Hashtable中
/// </summary>
/// <returns></returns>
public Hashtable GetPageInfo()
{
lzljLib.SqlDataBase sqlDB=new lzljLib.SqlDataBase(true);
Hashtable htPageInfo=new  Hashtable();
DataTable dtPageInfo=new DataTable(); 
try
{
sqlDB.fSqlCommand.Parameters.Clear();
sqlDB.OpenConnection();
dtPageInfo=sqlDB.GetDataTable("p_GetPageInfo");

}
catch(System.Data.SqlClient.SqlException ex)
{
throw (ex);
}
catch(Exception ex)
{
throw (ex);
}
finally
{
sqlDB.CloseConnection();
}

//获取所有页面信息:key与页号一一对应,路径,点击次数初始化为0



foreach( DataRow row in dtPageInfo.Rows)
{
ArrayList arrVisitInfo=new ArrayList(3);
arrVisitInfo.Add(row["PagePath"].ToString());
arrVisitInfo.Add(new DateTime());//最后一次点击的时间
arrVisitInfo.Add(0);//点击次数

int pageNO=Convert.ToInt32(row["PageNo"]);
htPageInfo.Add(pageNO,arrVisitInfo); //将页面编号作为key对应
}
return htPageInfo;
}
/// <summary>
/// 记录访问者信息:访问者IP,访问过的页面信息(包括:页面编号,及最后访问该页的时间,点击某页的次数)
/// </summary>
/// <param name="htVisitInfo"></param>
public void RecordVisitInfo(Hashtable htVisitInfo,string VisitorIP)
{
lzljLib.SqlDataBase sqlDB=new lzljLib.SqlDataBase(true);
try
{
int IP_ID;
//获取来访者IP的ID号
sqlDB.fSqlCommand.Parameters.Clear();
sqlDB.fSqlCommand.Parameters.Add("@IP_ID",SqlDbType.Int);
sqlDB.fSqlCommand.Parameters.Add("@IP",SqlDbType.VarChar,20);
sqlDB.fSqlCommand.Parameters["@IP"].Value=VisitorIP;
sqlDB.fSqlCommand.Parameters["@IP_ID"].Direction=ParameterDirection.Output; sqlDB.OpenConnection();
sqlDB.GetFirstColumn("p_GetVisitorIPID");
IP_ID=Convert.ToInt32(sqlDB.fSqlCommand.Parameters["@IP_ID"].Value); //TODO:存来访信息:IP,最后访问时间,访问过的页面,点击页面次数
sqlDB.fSqlCommand.Parameters.Clear();
sqlDB.fSqlCommand.Parameters.Add("@IP_ID",SqlDbType.Int);//IP
sqlDB.fSqlCommand.Parameters.Add("@PageNO",SqlDbType.VarChar,20);//页号
sqlDB.fSqlCommand.Parameters.Add("@LastTime",SqlDbType.DateTime);//最后一次访问时间
sqlDB.fSqlCommand.Parameters.Add("@ClickTimes",SqlDbType.Int);//单击该页次数

sqlDB.fSqlCommand.Parameters["@IP_ID"].Value=IP_ID;
foreach( int pageNO in htVisitInfo.Keys)
{
//只存点击过的页(点击次数大于0),
ArrayList arrVisitInfo=(ArrayList)htVisitInfo[pageNO];
if(Convert.ToInt32 (arrVisitInfo[2])>0)
{
sqlDB.fSqlCommand.Parameters["@PageNO"].Value=pageNO;
sqlDB.fSqlCommand.Parameters["@LastTime"].Value=arrVisitInfo[1];
sqlDB.fSqlCommand.Parameters["@ClickTimes"].Value=arrVisitInfo[2];
sqlDB.GetFirstColumn("p_RecordVisitInfo");
}
}
}
catch(System.Data.SqlClient.SqlException ex)
{
throw (ex);
}
finally
{
sqlDB.CloseConnection();
}
}
}
}

解决方案 »

  1.   

    这段代码能正常执行,可把它填入我的代码就不行了啊!!!!!!为什么,my god!!!!!!!!using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Web;
    using System.Web.SessionState;namespace TestWeb 
    {
    /// <summary>
    /// Global 篕璶磞瓃
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
    /// <summary>
    /// 砞璸ㄣ┮惠跑计
    /// </summary>
    private System.ComponentModel.IContainer components = null; public Global()
    {
    InitializeComponent();
    }

    protected void Application_Start(Object sender, EventArgs e)
    {
    Application["Count"] = 0;
    }
     
    protected void Session_Start(Object sender, EventArgs e)
    {
    int nCount = int.Parse(Application["Count"].ToString());
    nCount ++;
    Application["Count"] = Convert.ToString(nCount);
    Console.WriteLine(Application["Count"].ToString());
    } protected void Application_BeginRequest(Object sender, EventArgs e)
    { } protected void Application_EndRequest(Object sender, EventArgs e)
    { } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    { } protected void Application_Error(Object sender, EventArgs e)
    { } protected void Session_End(Object sender, EventArgs e)
    {
    int nCount = int.Parse(Application["Count"].ToString());
    nCount --;
    Application["Count"] = Convert.ToString(nCount);
    Console.WriteLine(Application["Count"].ToString());
    } protected void Application_End(Object sender, EventArgs e)
    { }

    #region Web Form 砞璸ㄣ玻ネ祘Α絏
    /// <summary>
    /// 砞璸ㄣや穿┮ゲ斗よ猭 - 叫づㄏノ祘Α絏絪胯竟э
    /// 硂よ猭ず甧
    /// </summary>
    private void InitializeComponent()
    {    
    this.components = new System.ComponentModel.Container();
    }
    #endregion
    }
    }