Run SQL queries at predefined time - c#

I am new to C#.
I have to write an application that would automatically run multiple predefined sql queries on multiple predefined databases at the same time every day. The application then has to take this information from the queries and compile it into 1 form/report, and then send this form/report to a predefined e-mail address.
All this must happen automatically every day at the same time.
I do know how to connect to a database and run queries on it in a C# application; I also know how to e-mail a form/ report from within a C# application.
My questions are:
How would it be possible to have this application run automatically at a predefined time every day?
How would I compile all this information into 1 form/report (I am using multiple databases and multiple queries)
I take it that this might be a console application?
I can’t find any references to this anywhere.
Any help would really be much appreciated.
Thanks in advance!

Don't try to build this logic yourself. I've done it a couple of times and it's just not worth it.
You should use the built in Windows Scheduler for this. You can set up when your application should run, how often it should reccur and much more. A console application or Windows service will do just fine.
This is hard to answer without knowing how your application is structured.Can't you just run your queries and combine the results into a report?

Windows scheduler or a unix cron job to run your app at a certain time is the usual way.

You can shedule a task for windows
http://support.microsoft.com/kb/308569
And you can megrge infogmation from several DataTables

Related

C# scheduler tools

I’m loath to ask another scheduler question here, I’ve read through dozens, but it’s still not clear to me what tools would best fit my need. I have three requirements for a reporting app:
User invoked
fixed scheduled
user scheduled.
I have an ASP.NET forms app to cover #1 and a C# console app to handle #2 but now #3 has been added to the mix.
So for the user scheduled reports I need to:
Present the user with a schedule selector and save their selection (into SQL Server?)
Have an app that checks the database for jobs to run/schedule
App to run the query and format the report
I suppose the latter two could be a single app but I’ve read it’s hard to debug service apps so keeping them separate may be good. I don’t know what parts of my requirements are met by Quartz.net and I’ve seen separate GUI tools (DayPilot) and backend (Task Manager API, CodePlex taskscheduler) mentioned. Not having used any of these I’m hoping to minimize my false starts.
If you require job scheduler try using hangfire.io
If you have SQL Server, then you should use SQL Server Reporting Services, which does all three.

Run a C# Application periodically to update database

I would like to know whether it is possible to run a C# console or ASP.NET application periodically.
My purpose is to automatically do the following steps once a month:
1) Query a Source database.
2) Take the result of the query at (1) and manipulate them by using LINQ and C#, for instance by checking if a certain value is already present in the Destination database.
3) Store the derived data in a Destination database.
The application has to run on a Windows Server 2008, the Source database is in a SQL 2005 Server and the Destination database is in a SQL 2008 Server.
I tried to create for instance a SSIS package but it won't do the job since I cannot add any logic.
Anybody has any suggestion?
You should create a Scheduled Task to perform this. Look here: Control Panel -> Administrative Tools -> Task scheduler
And as you stated, yes - a console app is highly recommended.
Edit
I agree with #andynormancx in that SSIS may be a better way to do this; however, it is commonly accepted to create a console app executed by a scheduled task. This is where it comes down to your resources, time, and expertise: it may or may not be worth the effort to learn enough about SSIS to create a package in SqlServer to do what you need. If someone were to give a complete answer using SSIS to perform this task, I would certainly bow to that expertise.
You can create a new Scheduled Task. It would be much easier and you don't have to re-invent the wheel.
You could create a scheduled task that will call your .exe at pre-defined interval.
Go to your control panel and select Scheduled Task and then add scheduled task
You can do it, of course. But I would recommend making it a Windows service.
If you really want to do it in C# I suggest you write a service, your code is almost Identical to a normal console app.
Check the following link to get started.
C# Service step by step

How to send automatic email on daily ,weekly and monthly bases?

How do I send email on daily, weekly and monthly bases using c#.net?
I was thinking of creating a windows service application but I don't know how to do it and if it's the right way to take.
Your thoughts are greatly appreciated.
A windows service is probably the best option.
In your service have a timer that fires with whatever resolution you need (every minute/hour/day etc) and on the timer tick even send your emails.
There are many tutorials for creating windows services with .NET.
A Windows service will do the trick. A good benefit of a Windows service is that it starts up when Windows starts up (or can be set to, anyway). So the machine can be left fairly unattended (as a good server should) and doesn't need anybody logged into it for the service to run. So if that's an important consideration for your scenario then perhaps a Windows service would be the way to go.
If you just want to create a console application instead (which can generally be easier to create/test/debug) then you can schedule it with the native Windows task scheduler. However, unless I'm mistaken, I think a user needs to be logged in to the machine in order for it to run. At least under certain circumstances. So a Windows service is probably your best bet for an unattended task.
As for creating the service, Visual Studio should have a project template for that. The scheduling would be handled with a Timer.
You could also use the Windows Task Scheduler. Quartz.Net seems to be the right tool, too (never used it though).
Windows Service is a good choice.
Remember to save state to disk (file or database) because service restart (for example, due to a reboot) is possible. For example, your next email delivery is a week in the future, you have to save that date to disk. So when your server is down due to a power failure 3 days later and recover after another day, your Windows Service can still set the right timer according to the date saved to disk.
Create a console application, which would be launched from the Windows task scheduler. Creating a windows service is unnecessarily complex, IMO, since you are simply having the program execute in response to time intervals; not other unpredictable external events where you would need a 'listener' type application. KISS!

C# run code every 30 days

If I were to run some code, perhaps send an email, every 30 days to users of my site, how would that be done?
Use Windows Task Scheduler to run your application.
Options:
1) Console app that runs in Windows Task Scheduler
2) Windows Service
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx
Which framework are you working with? I have some pointers if you're developing with .Net 4.0 like I am.
If you have access to your machine where you can install services I would utilize the new and improved Workflow Service for this situation. What's nice about them is that Workflows can persists for minutes, hours, days, weeks, months, etc. lying "dormant" until delays time periods finish.
If it's an IIS machine (and if that's the route you would like to take) it is relatively simple to build it straight as a "Workflow Service" project/solution in VS 2010. From there you are presented with a designer and several workflow activities in the toolbar.
Add a flowchart activity to place your email activities inside of. Flowchart is ideal because it can initialize whatever you need to and decision flow can redirect backwards in direction (as opposed to the always forward moving sequence activity). What you need to do with your logic is up to you from there since your question doesn't provide a lot of details.
Now if you're using .Net 3.5 then I would think about refraining from building Workflows since you have to migrate when switching to 4.0 (WF 3.5 is NOT compatible with 4.0). With admin access to the machine you can install a Windows Service that contains a timer to fire code every 30 days (or however defined) as needed.
If it's SQL Server specific, you can use SQL Jobs.
If you want to do it with your ASP.NET app instead of a separate app, you have some options:
Since you want it to run every 30 days instead of daily, I recommend this method of using a schedule table instead of application variables or cache to schedule jobs:
You can setup a schedule table in your db and check when it was last updated in your global.asax. On the Session start, check if the current date is more than 30 days. If it is, then call a method to send out emails.
If you send out emails, then update your schedule table with the current date.

What would be the best approach is my application had to pull information from a website every day at 2am

I'm making a small application that is supposed to download info from the web every day at 2am. It will download the information and write the strings to an XML file of my choosing.
Using .NET and C#.
My initial approach was to install a service on the users computer and have that run, but I'm not so sure. I've not even used it so much in the past, only once.
Which is the best (read: time tested :P ) approach to this very common problem.
You can either build your application as a Windows Service, as you mentioned.
Or else it would probably be a better idea to create a normal console application, and launch it automatically at 2.00am with the Windows Task Scheduler.
You can consider both methods as popular and "time-tested".
I would suggest having a console app, which calls data fetching algo in a separate public class (not the main method).
Like Daniel mentioned, run it via Windows Task Scheduler which itself will take care of most scheduling requirements.
This allows the solution to be scaled in the future if need be. E.g. convert into Windows Service, full GUI Winform or even SQL server scheduled tasks etc.

Categories

Resources