我在visual studio web express中有web应用程序,并且在sql server express中有db。 我想在每天下午5:00执行插入100条记录.web应用程序是在asp.net MVC和vb.net中开发的。并使用IIS 7.5部署在服务器计算机上。我应该遵循什么逻辑?
答案 0 :(得分:1)
对我来说,我正在使用这种方法,直到现在它都很好:)
我已经完成了要执行的任务以及重启任务的时间,这段时间如下:
public enum ScheduledTasks
{
CleanGameRequests = 120,
AnotherTask = 30,
}
然后我在Application_Start
中启动所有任务,以确保在我的应用程序运行时执行任务
protected void Application_Start()
{
...............
// Add the tasks on Application starts
AddTask(ScheduledTasks.CleanGameRequests);
AddTask(ScheduledTasks.AnotherTask);
}
现在好了,这就是诀窍:)
在AddTask方法中我只是添加新的空项来缓存,并根据任务时间为它设置AbsoluteExpiration,并为此任务调用合适的方法。
实际上,我无法解释这个想法,但这里是代码:
private static CacheItemRemovedCallback _onCacheRemove;
private void AddTask(ScheduledTasks task)
{
// Add my `CacheItemRemoved` method to be called on cache removed
_onCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
// Add new key to the cache with the name of this task
// and Expiration time acccordin to the task
HttpRuntime.Cache.Insert(task.ToString(), (int)task, null,
DateTime.Now.AddSeconds((int)task), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, _onCacheRemove);
}
然后,我要做的就是为CacheItemRemoved
方法中的每项任务选择合适的方法:
public void CacheItemRemoved(string key, object time, CacheItemRemovedReason r)
{
//Get Task enum object
var task = (ScheduledTasks)Enum.Parse(typeof(ScheduledTasks), key);
// Select the suitable method to depending on the Task Enum object
switch (task)
{
case ScheduledTasks.CleanGameRequests:
GameRequest.CleanUp();
break;
case ScheduledTasks.AnotherTask:
Service.AnotherTask();
break;
}
// Don't forget to re-add the task to the cache to do it again and again
AddTask(task);
}
最后一件事是检查时间是否是下午5:00,我建议您将此支票放入服务类。
希望这有助于你:)
答案 1 :(得分:0)
由于您使用的是 Sql server express 版本,因此无法在sql端创建预定作业。但你可以尝试其他选项,如。
Windows服务(如果您的托管服务提供商允许)