“触发"一定条件下使用C#/ASP.NET中的方法修改数据库 [英] "Triggering" a Method in C#/ASP.NET on a certain Condition to modify the Database

查看:54
本文介绍了“触发"一定条件下使用C#/ASP.NET中的方法修改数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我有一个关于在特定条件下触发事件或调用方法的问题(例如if(datetime1> datetime2))

基本上,我需要向生日那天午夜在我网站上注册的某人发送生日电子邮件.

然后,每个小时,我需要发送一封包含更新日志的PM给我.


我该怎么办?
我考虑过在母版页上使用LINQ查询,如果(DateTime.Now> Midnight),它会调用某种方法.但这可能会减慢所有网站的运行速度.

解决方案

这里基本上有两个选择.

最好是如果您拥有服务器或被允许在其上运行服务或cron作业(对不起,已调度的过程;)).将服务设置为每小时检查一次生日在最后一个小时内跳出的任何人,然后向您发送PM(即写入数据库表),或者等效地让每小时产生的进程执行一次. />
如果您不能这样做,也不能说服主机允许您这样做,则实际上可以检查每个请求并进行这种处理.例如,我的HTTP服务器类检查每个请求的会话超时.这种处理必须非常轻巧-在执行任何操作之前先检查一下是否已在本小时内完成.

实际上,考虑到这一点,ASP.net Web应用程序在一个持久性进程中运行.因此,您可能会在App_Load中产生一个与服务执行相同工作的线程(尽管您需要牢记同步问题&ndash ;,但由于并行处理Web请求,因此您应该已经是线程安全的) .不过,这似乎不是一个非常可靠的解决方案,因为无法保证IIS会无限期地将您的网站保留在单个进程中.


 如果( DateTime .现在>午夜)计入  a整天!
更好地创建服务,该服务在 午夜运行一次 span>生日电子邮件每小时   


创建一个将在MidNight执行的作业. 创建作业的步骤
现在在求职用户中,今天有生日并发送电子邮件.
要发送电子邮件,请使用数据库电子邮件此处,您会找到发送电子邮件的步骤


Hello.
I have a question about firing an event or call a method in a certain condition (eg if(datetime1 > datetime2) )

Basically I need to send birthday emails to someone registered on my website at his/her birthday at Midnight.

Then, each hour, I need to send a PM to me containing an updated log.


How do I do that?
I thought about using LINQ queries on masterpage, and if (DateTime.Now > Midnight) it calls a certain method. But it might slow down all the website.

Are there any solutions?

解决方案

There''s basically two options here.

Best is if you either own the server or are permitted to run a service or cron job (sorry, scheduled process ;) ) on it. Set the service up to check once an hour for anyone whose birthday tripped in the last hour, and send you a PM (i.e. write to a database table), or, equivalently, have the process which gets spawned every hour do that.

If you can''t do that, and you can''t persuade your host to let you do so, you can indeed check inside every request and do this sort of processing. For example my HTTP server class checks for session timeouts every request. This sort of processing needs to be very lightweight – a check before executing anything whether you did it this hour already.

Actually, thinking about it, an ASP.net web application runs in a persistent process. So you could potentially spawn a thread in App_Load which performed the same job as the service (though you will need to keep synchronisation issues in mind if you do that – but you should already be thread-safe as web requests are processed in parallel). That seems like not a very robust solution though, because there is no guarantee that IIS will keep your website in a single process indefinitely.


if (DateTime.Now > Midnight) counts for a complete day!
Better create a service that runs on midnight once for the birthday emails and hourly for the PM


Create a job that will execute at MidNight. Step To Create Job
Now in Job Find users which has birthday today and send email.
To send email use Database Email Here you find step to send email


这篇关于“触发"一定条件下使用C#/ASP.NET中的方法修改数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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