简体   繁体   English

如何调度数据库任务?

[英]How to schedule a database task?

Background: I created an application that measures a weight from a scale and saves the information in MS SQL server database.背景:我创建了一个应用程序,该应用程序通过秤测量重量并将信息保存在 MS SQL 服务器数据库中。

In the database, I want to save a record for the actual weight for every day as historical, and I don't know what I have to use to accomplish this task.在数据库中,我想将每天的实际体重记录保存为历史记录,我不知道我必须使用什么来完成这项任务。

Can a trigger in SQL Server do it?? SQL 服务器中的触发器可以做到吗?

You don't need to try and hack this with a trigger.您无需尝试使用触发器来破解它。 In SQL Server this type of feature (scheduled execution) is easily managed with a SQL Server Job .在 SQL 服务器中,这种类型的功能(计划执行)可以通过SQL 服务器作业轻松管理。 You would just create a SQL Server job that runs once a day and executes whatever T-SQL you need to meet the biz logic requirement.只需创建一个 SQL 服务器作业,该作业每天运行一次并执行满足 biz 逻辑要求所需的任何 T-SQL。

So you don't actually need to code anything else.因此,您实际上不需要编写任何其他代码。 Just configure a job and set its schedule.只需配置一个作业并设置它的时间表。 You do not need another application.您不需要其他应用程序。 SQL Server already manages scheduled execution with SQL Agent. SQL 服务器已使用 SQL 代理管理计划执行。

Do note that the SQL Server Jobs feature is not available with SQL Server Express editions.请注意,SQL Server Express 版本不提供 SQL 服务器作业功能。

Simple approach: use Windows Scheduler .简单的方法:使用Windows Scheduler

If this approach is too simple then create windows service and use Quartz.NET in your implementation to have trigger functionality.如果此方法过于简单,则创建 windows 服务并在您的实现中使用Quartz.NET以具有触发功能。

If you need to run a scheduled task/cron job in a shared hosting environment, you'll find a list of free web cron job providers here:ASP.NET: Free scheduling of your tasks (Cron jobs)如果您需要在共享主机环境中运行计划任务/cron 作业,您可以在此处找到免费 web cron 作业提供程序的列表:ASP.NET:免费安排您的任务(Cron 作业)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM