如何在ASP.NET中会话到期时运行更新查询 [英] How to run update query when session expire in ASP.NET

查看:66
本文介绍了如何在ASP.NET中会话到期时运行更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有桌子出勤有属性

[ID],[Engi_Profile_ID],[In_Date_Time],[Out_Date_Time],[Reason],[备注],[旗帜]



当我登录时,插入[int],[Engi_Profile_ID],[In_Date_Time],[In_Date_Only],

[原因],[备注],[旗帜]插入但是当会话到期时,会触发一个查询以进行更新,在哪里写它来触发更新查询是Global.asax页面还是web.config页面怎么做。

我知道我是什么做错了。

给我写回答。



我尝试了什么:



void Session_Start(对象发送者,EventArgs e)

{

HttpContext.Current.Session(Session_Name);

id = HttpContext.Current.Session.SessionID;

SqlCommand cmd1 = new SqlCommand(Update Attendence set Out_Date_Time ='+ DateTime.Now +',Flag ='关闭'where Engi_PRofile_ID ='+ txtID.Text +'和Flag ='打开'并替换(转换) (Nvarchar(20),In_Date_Only,106),'',' - ')=替换(转换(Nvarchar(20),gETDATE(),106),'',' - '),con);

cmd1.Parameters.AddWithValue(@ Engi_Profile_ID,txtID.Text.ToString());

}

解决方案

这样做的唯一方法是使用Session_End事件,但这只适用于进程内会话(这是默认情况下,你可能正在使用它们)但是它非常不可靠,你会发现它并不总是会触发。这只是你不应该做的事情之一,网络是无状态的,所以服务器不知道你是否在那里,所以不要构建任何依赖于知道用户何时不在的东西。

你可以使用Session_End来达到这个目的。



  void  Session_End( Object  sender,EventArgs E){
// 您的更新方法
}





Session_End事件不会触发当浏览器关闭时,当服务器在特定时间内没有收到用户的请求时(默认为20分钟),它会触发。


i have table Attendance having attribute
[ID],[Engi_Profile_ID],[In_Date_Time],[Out_Date_Time],[Reason],[Remark],[Flag]

When i login it insert [int],[Engi_Profile_ID] , [In_Date_Time] ,[In_Date_Only] ,
[Reason],[Remark],[Flag] is inserted but when session expire then a query is fired for update where to write it to fire the update query is it Global.asax page or web.config page how to do it.
I know what i have done is wrong.
give me write answer.

What I have tried:

void Session_Start(object sender, EventArgs e)
{
HttpContext.Current.Session("Session_Name");
id = HttpContext.Current.Session.SessionID;
SqlCommand cmd1 = new SqlCommand("Update Attendence set Out_Date_Time='" + DateTime.Now + "', Flag='Close' where Engi_PRofile_ID='" + txtID.Text + "' and Flag='Open' and Replace(Convert(Nvarchar(20),In_Date_Only,106),' ','-')=Replace(Convert(Nvarchar(20),gETDATE(),106),' ','-')", con);
cmd1.Parameters.AddWithValue("@Engi_Profile_ID", txtID.Text.ToString());
}

解决方案

The only way of doing this is to use the Session_End event, but that only works with in-process sessions (that's the default, you're probably using them) however it's very unreliable and you'll find it doesn't always fire. This is just one of those things you shouldn't do, the web is stateless so servers don't know if you are there or not so don't build anything that relies on knowing when the user is no longer there.


You can use Session_End to achieve this.

void Session_End(Object sender, EventArgs E) {
    // Your Update Method
}



The Session_End event doesn't fire when the browser is closed, it fires when the server hasn't gotten a request from the user in a specific time(by default 20 minutes).


这篇关于如何在ASP.NET中会话到期时运行更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆