Run a C# Application periodically to update database - c#

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

Related

scheduling a task c#

I have a C# console application. I would like to run that every hour of the day. This application is not intended to run on my local machine but on other users machine as a background process. I don't want the user to take the trouble of setting a task manually using task scheduler. How can I achieve the same for him ?
You can set it up to run on a timer with an interval of 1 hour.
See this link for examples
How do you add a timer to a C# console application
You could also install it as a windows service on the user's machine.
You can also use the Quartz.net scheduling service; in its simplest form it would install as a Windows Service and the schedule is configured in an XML file. You can then have it call your code at the scheduled time.
I kept some notes... not sure if this helps:
The Quartz.Net Guide I wish I had
Quartz.Net is very full featured, as far as I can read. If you use a scheduler so that you can defer tasks, or decouple a front-end from some rather long back-end processing, it can be done quite easily.
The large confusion for me was that Quartz can be used in many ways. You can embed it in your own app or service, etc. etc. The other confusion was that the official Tutorial could use some updating so that the code samples can work with the current version. Finally, the configuration seems pretty foreign for a .net user; I'm used to doing something like:
var myObject = new SuperUsefulClass();
myObject.ImportantSetting = SettingEnumeration.SomeValueThatIntellisenseSuggests;
myObject.OtherSetting = OtherSettingEnumeration.SomeValueThatIntellisenseSuggests;
Usually I like to explore a new class in LINQPad. I'll instantiate it as above, and play around with it until I am comfortable with using it in my project.
The configuration of Quartz.Net was about as foreign to me as imaginable. But that is because I overlooked one of the best pre-built options for using Quartz.Net, and I overlooked one of the best tutorials on Quartz.
For my use case, deferring tasks, I eventually realized the easiest way to use Quartz was with the pre-built Windows Service. To do that I simply modified an example configuration file, and I chose to persist all the jobs, job metadata, etc. to a SQL Server Database.
When it comes time for the job to be executed, the Quartz Windows Service will call the designated method (which of course implements the IJob interface). In order for the method to be called, your .dll or .exe ("assembly" in .net parlance) must be in the same folder where the service (Quartz.Service.exe) was installed. It then magically calls the method in your assembly. Of course if your method reads external files, those must also be copied to the service folder, or reachable somehow by the assembly.
After nearly giving up, I ran across Tarun Arora's tutorial. It is really excellent and since I cannot improve on it, I'll just link to it.
Install Quartz.Net as a windows service and Test installation
Quartz.Net Writing your first Hello World Job
Basically:
Tweak the quartz.config file (similar to the code above)
Install the Quartz.Net service (Quartz.Service.exe) as Administrator
Put assemblies (.exe or .dll) of code you want to execute, in the same folder as Quartz.Service.exe
Start the service
Make scheduling calls. i.e. your assembly code will make calls to the Quartz Service, which then persists and executes them on its
local system.
Create a service application: http://msdn.microsoft.com/en-us/library/zt39148a.aspx

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 SQL queries at predefined time

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

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.

MS Access interop - Data Import

I am working on a exe to export SQL to Access, we do not want to use DTS as we have multiple clients each exporting different views and the overhead to setup and maintain the DTS packages is too much.
*Edit: This process is automated for many clients every night, so the whole process has to be kicked off and controlled within a cursor in a stored procedure. This is because the data has to be filtered per project for the export.
I have tried many ways to get data out of SQL into Access and the most promising has been using Access interop and running a
doCmd.TransferDatabase(Access.AcDataTransferType.acImport...
I have hit a problem where I am importing from views, and running the import manually it seems the view does not start returning data fast enough, so access pops up a MessageBox dialog to say it has timed out.
I think this is happening in interop as well, but because it is hidden the method never returns!
Is there any way for me to prevent this message from popping up, or increasing the timeout of the import command?
My current plan of attack is to flatten the view into a table, then import from that table, then drop the flattened table.
Happy for any suggestions how to tackle this problem.
Edit:
Further info on what I am doing:
We have multiple clients which each have a standard data model. One of the 'modules' is a access exporter (sproc). It reads the views to export from a parameter table then exports. The views are filtered by project, and a access file is created for each project (every view has project field)
We are running SQL 2005 and are not moving to SQL 2005 quickly, we will probably jump to 2008 in quite a few months.
We then have a module execution job which executes the configured module on each database. There are many imports/exports/other jobs that run in this module execution, and the access exporter must be able to fit into this framework. So I need a generic SQL -> Access exporter which can be configured through our parameter framework.
Currently the sproc calls a exe I have written and my exe opens access via interop, I know this is bad for a server BUT the module execution is written so only a single module is executing at a time, so the procedure will never be running more than one instance at a time.
Have you tried using VBA? You have more options configuring connections, and I'm sure I've used a timeout adjustment in that context in the past.
Also, I've generally found it simplest just to query a view directly (as long as you can either connect with a nolock, or tolerate however long it takes to transfer); this might be a good reason to create the intermediate temp table.
There might also be benefit to opening Acces explicitly in single-user mode for this stuff.
We've done this using ADO to connect to both source and destination data. You can set connection and command timeout values as required and read/append to each recordset.
No particularly quick but we were able to leave it running overnight
I have settled on a way to do this.
http://support.microsoft.com/kb/317114 describes the basic steps to start the access process.
I have made the Process a class variable instead of a local variable of the ShellGetApp method. This way when I call the Quit function for access, if it doesn't close for whatever reason I can kill the process explicitly.
app.Quit(Access.AcQuitOption.acQuitSaveAll);
if (!accessProcess.HasExited)
{
Console.WriteLine("Access did not exit after being asked nicely, killing process manually");
accessProcess.Kill();
}
I then have used a method timeout function here to give the access call a timeout. If it times out I can kill the access process as well (timeout could be due to a dialog window popping up and I do not want the process to hang forever. I got the timeout method here.
Implement C# Generic Timeout
I'm glad you have a solution that works for you. For the benefit of others reading this, I'll mention that SSIS would have been a possible solution to this problem. Note that the difference between SSIS and DTS is pretty much night and day.
It is not difficult to parameterize the export process, such that for each client, you could export a different set of views. You could loop over the lines of a text file having the view names in it, or use a query against a configuration database to get the list of views. Otherparameters could come from the same configuration database, on a per-client and/or per-view basis.
If necessary, there would also be the option of performing per-client pre- and post-processing, by executing a child process, or pacakge, if such is configured.

Categories

Resources