如何使用quartz.net将数据插入或更新到数据库

时间:2014-03-18 04:08:04

标签: c# mysql quartz.net

我有一个程序,使用quartz.net每月自动更新或插入数据库中的数据。 这是代码:

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++;
        }
    }
}

但是当我运行代码时,它无法运行。是否可以使用quartz.net来更新数据库中的数据?

1 个答案:

答案 0 :(得分:0)

作业调度程序只执行IJob或多个IJob。它与作业的运行无关。

您可以手动测试您的工作。

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

(注意,既然你的IJob没有引用“context”变量,上面应该运行正常。)

如果你的简单测试工作(使用上面的代码)....但是当它被Quartz.Net调度程序触发时它不起作用........然后我会检查连接字符串。 它可能会推断出不同的身份(域\用户名)。我猜这里。

首先在调度程序之外测试代码。然后将其连接到调度程序。