简体   繁体   中英

How to insert or update data to database using quartz.net

i have a program that using quartz.net to update or insert data in the database automatically every month. here is the code :

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter {Level = Common.Logging.LogLevel.Info};

            // Grab the Scheduler instance from the Factory 
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

            // and start it off
            scheduler.Start();

            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<HelloJob>()
                .WithIdentity("job1", "group1")
                .Build();

            // Trigger the job to run now, and then repeat every 10 seconds
            ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .WithSchedule(CronScheduleBuilder.MonthlyOnDayAndHourAndMinute(1,01,00))
            .Build();

            // Tell quartz to schedule the job using our trigger
            scheduler.ScheduleJob(job, trigger);

            // some sleep to show what's happening
            //Thread.Sleep(TimeSpan.FromSeconds(60));

            // and last shut down the scheduler when you are ready to close your program
            //scheduler.Shutdown();
        }
        catch (SchedulerException se)
        {
            Console.WriteLine(se);
        }

        Console.WriteLine("Press any key to close the application");
        Console.Read();
    }
    }
    }





public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {


        //select semua data 
        OdbcConnection conn = new OdbcConnection("Dsn=Mysql;Database=depresiasi");
        OdbcCommand comm = new OdbcCommand("select * from aktiva_tetap",conn);
        conn.Open();
        DataSet ds = new DataSet();
        OdbcDataAdapter da = new OdbcDataAdapter(comm);
        da.Fill(ds);
        conn.Close();

        int i = 0;

        while (i < (ds.Tables[0].Rows.Count - 1))
        {
            //persiapan insert data ke tabel detail_depresiasi

            decimal bebanpenyusutan = (decimal.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString()) - decimal.Parse(ds.Tables[0].Rows[i].ItemArray[6].ToString())) / int.Parse(ds.Tables[0].Rows[i].ItemArray[5].ToString());
            decimal akumulasipenyusutan = 0;

            //perhitungan akumulasi penyusutan
            OdbcCommand comm3 = new OdbcCommand("select * from detail_depresiasi where id_aktivatetap='" + ds.Tables[0].Rows[i].ItemArray[0].ToString() + "'", conn);
            conn.Open();
            DataSet ds3 = new DataSet();
            OdbcDataAdapter da3 = new OdbcDataAdapter(comm);
            da3.Fill(ds3);
            conn.Close();

            if (ds3.Tables[0].Rows.Count > 1)
            {
                int y = 0;
                while (y < ds3.Tables[0].Rows.Count - 1)
                {
                    akumulasipenyusutan += decimal.Parse(ds3.Tables[0].Rows[y].ItemArray[4].ToString());
                    y++;
                }
            }
            else
            {
                akumulasipenyusutan = bebanpenyusutan;
            }

            decimal nilaibuku = decimal.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString()) - akumulasipenyusutan;
            //insert data ke tabel detail_depresiasi
            OdbcCommand comm2 = new OdbcCommand("insert into detail_depresiasi values('" + ds.Tables[0].Rows[i].ItemArray[0].ToString() + "','" + DateTime.Now.Month.ToString() + "'," + decimal.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString()) + "," + bebanpenyusutan + "," + akumulasipenyusutan + "," + nilaibuku + ")", conn);
            conn.Open();
            DataSet ds2 = new DataSet();
            OdbcDataAdapter da2 = new OdbcDataAdapter(comm);
            da2.Fill(ds2);
            conn.Close();

            i++;
        }
    }
}

but when i run the code it's not working. is possible to use quartz.net to update data in the database ?

The job scheduler simply executes an IJob or multiple IJob's. It has nothing to do with if the the Job will run.

You can test your job MANUALLY.

IJob myJob = new HelloJob();
myJob.Execute(null);

(note, since your IJob does not refer to the "context" variable, the above should run fine.)

If your simple test works (using the code above)....but it does not work when being fired by the Quartz.Net scheduler........then I would check the connection string. It ~may~ be inferring a different identity (domain\\username). I am guessing here.

Test your code outside of scheduler first. Then wire it up to the scheduler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM