c# - 如何使用quartz.net向数据库插入或更新数据

标签 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更新数据库中的数据吗?

最佳答案

作业调度程序仅执行一个或多个 IJob。它与作业是否运行无关。

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

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

(注意,由于您的 IJob 没有引用“上下文”变量,因此上面的代码应该可以正常运行。)

如果您的简单测试有效(使用上面的代码)......但在被 Quartz.Net 调度程序触发时它不起作用......那么我将检查连接字符串。 它〜可能〜推断出不同的身份(域\用户名)。我在这里猜测。

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

关于c# - 如何使用quartz.net向数据库插入或更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470187/

相关文章:

c# - 这对 IDisposable 是否正确?

c# - 如何创建无需用户交互即可自行更新的 Windows 服务

c# - Quartz.Net在Mono中启动线程卡住系统

c# - 尝试将 Quartz.NET 与 oWin 一起使用时出现 Quartz.Simpl.JsonObjectSerializer 错误

c# - Nhibernate 和 MySQL 的 BLOB 索引前缀长度

c# - 包含对象实例的静态方法,错了吗?

php - mysqli fetch_array 不迭代

php - 在 Php 数组中搜索字符串并提取相邻文本

python - 如何在 pythonanywhere 上设置 Django MySQL 数据库?

Quartz.Net:防止不同作业类型同时运行