ASP.net中网站访问量统计方法代码(2)

public IPControl()
{
}
///
/// 保存IP数据信息到数据库
///
///
///
public void AddIP(string ipAddress,string ipSrc,DateTime ipDatetime)
{
//构建连接语句字符串
StringBuilder strSQL = new StringBuilder();
//创建表示QQ号的参数
SqlParameter[] parms = new SqlParameter[] { new SqlParameter(PARM_IP_ADDRESS, SqlDbType.NVarChar, 20),
new SqlParameter(PARM_IP_SRC, SqlDbType.NVarChar,80),
new SqlParameter(PARM_IP_DATETIME, SqlDbType.DateTime)};
SqlCommand cmd = new SqlCommand();

// 依次给参数赋值,并添加到执行语句中
parms[0].Value = ipAddress;
parms[1].Value = ipSrc;
parms[2].Value = ipDatetime;
foreach(SqlParameter parm in parms)
cmd.Parameters.Add(parm);

//定义对象资源保存的范围,一旦using范围结束,将释放对方所占的资源
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
{
//在执行字符串中加载插入语句
strSQL.Append(SQL_INSERT_IPSTAT);
conn.Open();

//设定SqlCommand的属性
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSQL.ToString();
//执行SqlCommand命令
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
//如果执行成功,返回true,否则false。
}
}
public string GetTotal()
{
//调用SqlHelper访问组件的方法返回第一行第一列的值
object count = SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_TOTAL, null);
//返回统计结果
return count.ToString();
}
public string GetToday()
{
//调用SqlHelper访问组件的方法返回第一行第一列的值
object count = SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_TODAY, null);
//返回统计结果
return count.ToString();
}
public string GetYesterday()
{
//调用SqlHelper访问组件的方法返回第一行第一列的值
object count = SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_YESTERDAY, null);
//返回统计结果
return count.ToString();
}
public string GetMonth()
{
//调用SqlHelper访问组件的方法返回第一行第一列的值
object count = SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_MONTH, null);
//返回统计结果
return count.ToString();
}
}

在Global.asax中使用定时器来统计在线人数和每天每月的访问量

一、在 Application_Start 中创建定时器

复制代码 代码如下:


//以下为使用多个定时器System.Timers.Timer的处理方法
//用thread重新包一下,定义两个定时器
System.Threading.Thread myTimer_1 = new System.Threading.Thread(new System.Threading.ThreadStart(write_1));
myTimer_1.Start();
System.Threading.Thread myTimer_2 = new System.Threading.Thread(new System.Threading.ThreadStart(write_2));
myTimer_2.Start();


二、使用定时器每10分钟更新一次在线人数检查一次是否要存入一天流量的信息

复制代码 代码如下:


//使用第一个定时器,每10分钟更新一次在线人数
private void write_1()
{
//以下使用System.Timers.Timer类 每间隔10分钟存一次数据
System.Timers.Timer myTimer1 = new System.Timers.Timer(600000); //实例化Timer类,设置间隔时间为600000毫秒(10分钟存一次总人数);
myTimer1.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
myTimer1.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed); //到达时间的时候执行事件myTimer_Elapsed;
myTimer1.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
}
//使用第二个定时器,
private void write_2()
{
//以下使用System.Timers.Timer类 每间隔10分钟检查一次是否要存入一天流量的信息
System.Timers.Timer myTimer2 = new System.Timers.Timer(600000); //实例化Timer类,设置间隔时间为600000毫秒(10分钟存一次总人数);
myTimer2.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_peopleDay); //到达时间的时候执行事件myTimer_peopleDay;
myTimer2.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
}

三、创建 myTimer过程来处理在线人数和统计每日、月、年的流量

复制代码 代码如下:


//创建 myTimer_Elapsed 过程并定义第一个定时器事件,要用来处理在线人数的代码
private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//如果现在的在现人数大于原有的在现人数,则替换数据表中的在现人数
int MaxOnline = Convert.ToInt32(Application["OnlineMax"]);
int MinOnline = Convert.ToInt32(Application["OnlineWhx"]);
if (MaxOnline < MinOnline)
{
SqlConnection con = Db.DB.createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("update countpeople set totol='" + Application["countSession"].ToString() + "',OnLine=+'" + Application["onlineWhx"] + "',DataTimes='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'", con);
cmd.ExecuteNonQuery();
con.Close();
Application["OnlineMax"] = Application["OnlineWhx"]; //将现在线人数赋于OnlineMax
Application["dataTimes"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
//将总访问人数写入数据库
SqlConnection con = Db.DB.createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("update countpeople set totol=" + Application["countSession"].ToString(), con);
cmd.ExecuteNonQuery();
con.Close();
}
}
//创建 myTimer_peopleDay 过程并定义第二个定时器事件,要用来统计每日、月、年的流量
private void myTimer_peopleDay(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
//当天晚上24时
if (DateTime.Now.Hour == 23)
{
if (DateTime.Now.Minute >= 50)
{
//当天晚上24时,写入一天的流量
//初始化一个iP数据访问对象
IPControl cont = new IPControl();
//获取今天访问量
Int32 countToday = Convert.ToInt32(cont.GetToday());
//获取本月访问量
Int32 countMonth = Convert.ToInt32(cont.GetMonth());

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgwzg.html