sql - Schedule Windows Service to run once daily -
i have stored procedure written in sql server 2008 r2 needs run daily. stored procedure accepts parameters 2 dates start date
& end date
, based on these dates fetches data table a
, writes table b
.
in order automate task of running stored procedure daily, have written windows service. have scheduled task in service1.cs
follows:
system.timers.timer otimer = null; public serviceexample() { initializecomponent(); otimer = new system.timers.timer(); settimer(); } private void settimer() { datetime currenttime = datetime.now; int intervaltoelapse = 0; datetime scheduletime = new datetime(currenttime.year, currenttime.month, currenttime.day, 8, 0, 0); //run @ 8 every morning if (currenttime <= scheduletime) intervaltoelapse = (int)scheduletime.subtract(currenttime).totalseconds; else intervaltoelapse = (int)scheduletime.adddays(1).subtract(currenttime).totalseconds; otimer = new system.timers.timer(intervaltoelapse); otimer.elapsed += new system.timers.elapsedeventhandler(otimer_elapsed); otimer.start(); } void otimer_elapsed(object sender, system.timers.elapsedeventargs e) { //make connection sql , call sp dblibrary odblibrary = new dblibrary(); dataset dscustomer = odblibrary.getcustomerdetails(); otimer.interval = (24 * 60 * 60 * 1000); } protected override void onstart(string[] args) { settimer(); } protected override void onstop() { otimer.stop(); }
the call stored procedure follows:
public dataset getcustomerdetails() { datetime first_date = datetime.now; datetime dateonly = first_date.date; datetime first_date1 = dateonly.adddays(-1); // assign see new instance datetime end_date = datetime.now; sqlconnection osqlconnection = new sqlconnection(strconn); osqlconnection.open(); sqlcommand osqlcommand = new sqlcommand(); osqlcommand.commandtimeout = 0; osqlcommand.commandtype = commandtype.storedprocedure; osqlcommand.commandtext = "daily_airtime_summary"; osqlcommand.parameters.add(new sqlparameter("@startdate", sqldbtype.datetime)).value = first_date1; osqlcommand.parameters.add(new sqlparameter("@enddate", sqldbtype.datetime)).value = end_date; osqlcommand.connection = osqlconnection; dataset ds = new dataset(); sqldataadapter osqldataadapter = new sqldataadapter(osqlcommand); osqldataadapter.fill(ds); osqlconnection.close(); return ds; }
the issues facing:
- even though scheduled time 8 , debug service @ around 10 am, starts run (execute stored procedure) immediately.
- the idea call/run stored procedure
once
. however, when checktable b
, see same data being populated 17 times. - the dates passed in parameters
start date = yesterday's date
,end date = today's date
. however, see procedure running continuously takingstart date = today's date
once datestart date = yesterday's date
,end date = today's date
has been fetched (multiple times).
can guide here please?
check logic time run proc. may not initialized causing proc torrun on initial start.
also, debug need install service using the. net utility points service executable @ debug version in project. complie in debug mode, start service windows service utility in control panel, in visual studio attach process.
Comments
Post a Comment