C# 如何使用quartz.net向数据库插入或更新数据
我有一个程序,使用quartz.net每月自动更新或插入数据库中的数据。 代码如下:C# 如何使用quartz.net向数据库插入或更新数据,c#,mysql,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.Loggin
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++;
}
}
}
private void Form1\u加载(对象发送方,事件参数e)
{
尝试
{
Common.Logging.LogManager.Adapter=new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter{Level=Common.Logging.LogLevel.Info};
//从工厂获取调度程序实例
isScheduler scheduler=StdSchedulerFactory.GetDefaultScheduler();
//然后开始
scheduler.Start();
//定义作业并将其绑定到HelloJob类
IJobDetail job=JobBuilder.Create()
.WithIdentity(“工作1”、“组1”)
.Build();
//立即触发作业运行,然后每10秒重复一次
ITrigger trigger=TriggerBuilder.Create()
.WithIdentity(“触发器1”、“组1”)
.具有时间表(CronScheduleBuilder.月、日、小时和分钟(1,01,00))
.Build();
//告诉quartz使用触发器安排作业
ScheduleJob(作业,触发器);
//一些睡眠来显示发生了什么
//线程睡眠(时间跨度从秒(60));
//最后在准备关闭程序时关闭计划程序
//scheduler.Shutdown();
}
捕获(调度)
{
控制台写入线(se);
}
Console.WriteLine(“按任意键关闭应用程序”);
Console.Read();
}
}
}
公共类HelloJob:IJob
{
public void Execute(IJobExecutionContext上下文)
{
//选择semua数据
OdbcConnection conn=newodbcconnection(“Dsn=Mysql;Database=depresiasi”);
OdbcCommand comm=新的OdbcCommand(“从aktiva_tetap中选择*”,康涅狄格州);
conn.Open();
数据集ds=新数据集();
OdbcDataAdapter da=新的OdbcDataAdapter(comm);
da.填充(ds);
康涅狄格州关闭();
int i=0;
而(i<(ds.Tables[0].Rows.Count-1))
{
//PERSIPAN插入数据表详细信息
decimal bebanpenyusutan=(decimal.Parse(ds.Tables[0]。行[i]。ItemArray[2]。ToString())-decimal.Parse(ds.Tables[0]。行[i]。行[i]。ToString())/int.Parse(ds.Tables[0]。行[i]。ItemArray[5]。ToString());
十进制akumulasipenyusutan=0;
//未来的未来
OdbcCommand comm3=新OdbcCommand(“从详细信息中选择*,其中id_-aktivatetap=”)+ds.Tables[0]。Rows[i]。ItemArray[0]。ToString()+”,conn);
conn.Open();
数据集ds3=新数据集();
OdbcDataAdapter da3=新的OdbcDataAdapter(comm);
da3.填充(ds3);
康涅狄格州关闭();
如果(ds3.Tables[0].Rows.Count>1)
{
int y=0;
而(y
但是当我运行代码时,它不起作用。可以使用quartz.net更新数据库中的数据吗?作业调度器只执行一个IJob或多个IJob。这与作业是否运行无关 您可以手动测试您的作业
IJob myJob = new HelloJob();
myJob.Execute(null);
(注意,由于IJob没有引用“context”变量,因此上述操作应该可以正常运行。)
如果您的简单测试可以工作(使用上面的代码)…但是在被Quartz.Net调度器触发时它不工作……。那么我将检查连接字符串。
它可能推断出不同的身份(域\用户名)。我在这里猜
首先在调度程序外部测试代码。然后将其连接到调度程序。请输入相关代码,而不是发布整个文件。相关代码的意思是,发布导致问题的部分,将表单作为计划作业进行感谢,看起来很奇怪。另外,您会得到什么错误或异常?它没有错误,只是不工作。HelloJob类中的代码不工作。我想在数据库中插入和更新一些表。可以用quartz.net实现吗?或者这是做这件事的其他方法吗??