简体   繁体   English

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

[英]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. 我有一个程序,每个月使用quartz.net自动更新或在数据库中插入数据。 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 ? 是否可以使用quartz.net更新数据库中的数据?

The job scheduler simply executes an IJob or multiple IJob's. 作业调度程序仅执行一个或多个IJob。 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.) (请注意,由于您的IJob没有引用“ context”变量,因此上面的命令应该可以正常运行。)

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. 如果您的简单测试有效(使用上面的代码)....但是当它被Quartz.Net调度程序激发时不起作用........那么我将检查连接字符串。 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. 然后将其连接到调度程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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