如何统计某次登录的时长

首先在页面前台加入如下代码:

 <script>
function window.onbeforeunload() {
  if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
  {
    _Default.insertExitTime();
    alert("关闭时的操作");
   
  }else {
  _Default.insertExitTime();
    alert("你在刷新")
  }
}
</script>

在js代码中使用ajaxpro组件调用后台方法对数库进行操作,插入退出系统时间和此次录登录系统总时长代码如下:

    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public void insertExitTime()
    {
        string sqlconn="server=.\\SqlExpress;database=PowerOA;Integrated Security=True";
        string strsql = "update LoginLog set ExitDateTime='" + DateTime.Now + "',LoginDateLength=20 where userid='" + Session["userid"] + "' ";
        SqlConnection conn = new SqlConnection(sqlconn);
        conn.Open();
        SqlCommand cmd = new SqlCommand(strsql, conn);
        cmd.CommandType = CommandType.Text;
        int t= cmd.ExecuteNonQuery();
        int a = t;
        conn.Close();
    }

posted @ 2009-06-26 11:31  XGU_Winner  阅读(283)  评论(0编辑  收藏  举报