簡體   English   中英

如何使用quartz.net將數據插入或更新到數據庫

[英]How to insert or update data to database using 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更新數據庫中的數據?

作業調度程序僅執行一個或多個IJob。 與作業是否運行無關。

您可以手動測試您的工作。

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

(請注意,由於您的IJob沒有引用“ context”變量,因此上面的命令應該可以正常運行。)

如果您的簡單測試有效(使用上面的代碼)....但是當它被Quartz.Net調度程序激發時不起作用........那么我將檢查連接字符串。 它可能會推斷出其他身份(域\\用戶名)。 我猜在這里。

首先在調度程序之外測試代碼。 然后將其連接到調度程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM