Ken D
Ken D

Reputation: 5968

The best way of triggering a timely event

I am building an ASP.Net website, I have a db table which I want to reset a column in it for all rows. column type: byte

I want to do this every day at midnight, in an automatically way. I know I can set a job in SQL Server Management Studio but I want to do this in a programmatic way and my website will be the trigger for it.

I'm using C#.Net 2008 and MS SQL Server 2005

i.e. (Pseudo code)

if(new_day)// can we be accurate that time here will be around 12:00:05 at maximum?
    // call sql stored procedure to reset that column

Upvotes: 1

Views: 447

Answers (4)

Martin Liversage
Martin Liversage

Reputation: 106906

Your website only executes when a page is requested making it unable to act as a service that executes a task at regular intervals. The best solution is to use SQL Server directly to schedule the task, create your own service or execute an application at regular intervals using Windows task scheduler.

However, if you for are in hosted environment you may not be able to do any of this. In that case you can use the cache to simulate a service. Omar Al Zabir has an article on CodeProject that explains how you can do that: Simulate a Windows Service using ASP.NET to run scheduled jobs.

Upvotes: 1

Pavel Urbančík
Pavel Urbančík

Reputation: 1496

SQL Agent job, scheduled to run at the specified time.

Upvotes: 0

J Benjamin
J Benjamin

Reputation: 4782

You could make a simple Windows service in C# that would run in the background but considering all you want to do is make database updates, it's best to keep it in the DB....I'd suggest going with the scheduled job in SQL

Upvotes: 2

Jan Jongboom
Jan Jongboom

Reputation: 27342

If you really want to: create a Scheduled Task that calls your website every day at midnight. The website itself cannot trigger itself, but a task can do this.

But really: just set up a SQL job.

Upvotes: 1

Related Questions