using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using pweb.data.OleDb;
using pweb.page;
using pweb.pagebase;
using pweb.user;
public class online : pweb_base
{
public online()
{
if (Session["sessionid"] == null)
{
Session["sessionid"] = mySessionID();
}
}
public void UpData()
{
DeleteUserOfTimeout();
douser();
}
/// <summary>
/// 删除超过Session时长的用户资料
/// </summary>
protected void DeleteUserOfTimeout()
{
int SessionTimeout=Session.Timeout;
pweb_data_OleDb pwebdata = new pweb_data_OleDb();
string sql = "delete * from online where astime<#" + DateTime.Now.AddMinutes(-SessionTimeout) + "#";
//Response.Write(sql);
pwebdata.executesql(sql);
}
/// <summary>
/// 返回当前用户的SessionID
/// </summary>
/// <returns></returns>
protected string mySessionID()
{
//Response.Write(Session.SessionID);
return Session.SessionID;
}
/// <summary>
/// 返回用户名
/// </summary>
/// <returns></returns>
protected string myUID()
{
pwebuserclass pwebuser=new pwebuserclass();
if (pwebuser.IsUserLogin())
{
return pwebuser.username;
}
else
{
return "游客";
}
}
/// <summary>
/// 判断sessesid是否存在数据表中
/// </summary>
/// <returns></returns>
protected bool checkSessionID()
{
string sql = "select * from online where Sessionid='" + Session["Sessionid"].ToString() + "'";
pweb_data_OleDb pwebdata = new pweb_data_OleDb();
if (pwebdata.retable(sql).Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
protected void douser()
{
string sql;
pweb_data_OleDb pwebdata = new pweb_data_OleDb();
if (checkSessionID())
{
sql = "update online set uid='" + myUID() + "', astime=#" + DateTime.Now + "# where sessionid='" + Session["Sessionid"].ToString() + "'";
pwebdata.executesql(sql);
}
else
{
sql = "insert into online (uid,sessionid) values('" + myUID() + "','" + Session["Sessionid"].ToString() + "')";
pwebdata.executesql(sql);
}
}
}
///本类使用a数据库
格式如下:
uid 文本 250
sessionid 文本 250
astimes 日期时间 默认值:Now()
使用示范:
online onlinuser = new online();
onlinuser.UpData();
pweb_data_OleDb pwebdata = new pweb_data_OleDb();
DataTable dt = pwebdata.retable("select uid from online");
foreach (DataRow dr in dt.Rows)
{
Response.Write(dr["uid"].ToString()+"<br>");
}
本类的数据查询和数据操作都已经写在 pweb.data.OleDb;
根据自己的需要做修改
本类用于.net 2.0 并只能用于Login登陆控件。
谢谢支持