Windows服务或计划任务

Windows服务或计划任务

问题描述:

我是一名ASP.NET开发人员,目前我正在使用C#和SQL Server。



我希望有一个自动化过程,它可以执行以下操作: -

[1]连接到SQL Server

[2]从表中选择FTP数据(主机,用户名和密码)和提取数据的时间>
[3]基于提取数据的时间(例如上午6:00),它应该使用所选的FTP数据连接到FTP服务器

[4]全部下载FTP服务器中可用的文件到本地系统。

[5]这个过程应该连续执行,因为步骤[2]将选择多行不同的FTP数据并且它是相应的抽取数据的时间。



说完上述要求后,我的澄清是: -

[1]我应该使用Windows服务吗?或计划任务

[2]如果是Windows服务,那么如何实现上述要求,即连接到数据库,提取数据,将FTP连接为上述时间点,将可用数据下载到本地系统和全天候进程?

[3]如果计划任务那么如何使用.Net开发计划任务的模块任务并将该模块集成到计划任务中。



我们非常感谢您及时提供示例代码的指导。



Mohandas K

I am a ASP.NET developer and at present I work with C# and SQL Server.

I would like to have an automated process which does the following:-
[1] Connect to SQL Server
[2] Select FTP data (host, username and password) and "time to pull data" from a table
[3] Based on the "time to pull data" (say example 6:00am), it should connect to the FTP server using the selected FTP data
[4] Download all the files available in the FTP server to the local system.
[5] And this process should be executed continuously since, the step [2] will select more than one row with different FTP data and it's corresponding "time to pull data".

Having said the above requirements, my clarification is:-
[1] Should I use Windows Service or Schedule Task
[2] If "Windows Service" then how can the above requirement will be achieved, that is connecting to database, pulling data, connecting FTP as a said point of time, downloading available data to local system and the process round the clock?
[3] If "Schedule Task" then how can I develop modules task for "Schedule Task" using .Net and integrate that module in "Schedule Task".

Your timely guidance with sample code will be highly appreciated.

Mohandas K

我会随时选择计划任务。在早些时候,我确实使用了Windows服务。但是我一次又一次意识到计划任务非常简单直接。这是因为,在Windows服务的情况下,您需要注意它的启动模式是自动还是手动,如果它的手册,你很难启动服务,忘记哪些可能会导致不必要的问题。当然,您可以将其设置为自动,以便在重新启动计算机时启动服务。



但是你需要花费很多时间来处理运行服务等等。另一方面,如果它是Windows中的预定任务,您无需担心启动模式或Windows服务相关功能,只需专注于应用程序的实际任务。



如果您计划执行计划任务,则只需开发基于控制台的应用程序即可完成所需操作。您还可以将控制台窗口设置为隐藏,以便在计划时间到达时不会出现控制台窗口,因为这可能会使该系统的用户(或您)感到烦恼。



PS - 这只是我的意见,如果你想要有所不同,请添加为什么作为评论,因为这也可以为其他人提供信息。谢谢!



希望这会有所帮助!
I would choose a scheduled task for its simplicity anytime. In the earlier days I did use windows services. But time and again I have realized that a scheduled task is pretty simple and straight-forward. Thats because, in the case of a windows service, you need take care of whether its startup mode is automatic or manual and if its manual, you have the pain of starting the service, forgetting which could cause you unwanted issues. Of course you could set it to be automatic, such that the service starts when your machine is restarted.

But you have the overhead of taking care of when to run the service and so on. On the other hand if it's a scheduled task in windows, you need not worry about the startup modes or the windows service related functionality and just concentrate in the actual task of your application.

If you plan to have a scheduled task, you just need to develop a console based application that does what you need. You could also set up the console window to be hidden, so that the console window does not appear when the scheduled time arrives, as it could be annoying to the users (or you) of that system.

PS - This is just my opinion, if you like to differ, please add why as a comment as this could be informative for others too. Thanks!

Hope this helps!


当您希望连接到SQL Server以启动此过程时,我'实际上,我希望在SQL Server中安排任务。看看这里 [ ^ ]有关如何创建定期计划任务的详细信息。这种方法的优点在于您仍然可以在SQL Server中执行各种聪明的.NET事务,但是您可以删除其他系统上的依赖项。
As you are looking to connect to SQL Server to start this process off, I'd actually look to schedule the task inside SQL Server. Have a look here[^] for details on how to create a regularly scheduled task. The beauty of this approach is that you can still do all sorts of clever .NET things inside SQL Server, but you remove the dependencies on other systems.