我使用Quartz.Net来安排工作。请看我的代码。为什么会出现此错误?
我需要在数据库中注册数据。
错误:
我使用此代码注册数据:
public class NewsSchedule : INewsSchedule
{
public void Run(int minute)
{
DateTimeOffset startTime = DateBuilder.FutureDate(2, IntervalUnit.Second);
IJobDetail job = JobBuilder.Create<NewsJob>()
.WithIdentity("newsJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1")
.StartAt(startTime)
.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(job, trigger);
sc.Start();
}
}
public interface INewsSchedule
{
void Run(int minute);
}
public class NewsJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=NewsApk;Integrated Security=SSPI;");
sqlConnection.Open();
var sqlCommand = new SqlCommand("Insert into JN_NewsCategories(NewsCategoriesFr,NewsCategoriesEn,IsGetNews)Values(@1,@2,@3)");
sqlCommand.Parameters.AddWithValue("@1", "test");
sqlCommand.Parameters.AddWithValue("@2", "test2");
sqlCommand.Parameters.AddWithValue("@3", false);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
更新:
请参阅:
Select命令正常工作,但是register命令不起作用。
public class NewsJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var sqlConnection =
new SqlConnection("Data Source=.;Initial Catalog=NewsApk;Integrated Security=SSPI;");
sqlConnection.Open();
var sqlCommand =
new SqlCommand(
"Insert into JN_NewsCategories(NewsCategoriesFa,NewsCategoriesEn,IsGetNews)Values(@1,@2,@3)");
sqlCommand.Parameters.AddWithValue("@1", "test");
sqlCommand.Parameters.AddWithValue("@2", "test2");
sqlCommand.Parameters.AddWithValue("@3", false);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from JN_NewsCategories", sqlConnection);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);// 12 record found. its ok.
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
谢谢@ K.B。我发现错误。 &#34; Message =&#34; ExecuteNonQuery:Connection属性尚未初始化。&#34;。为什么会出现此错误?
答案 0 :(得分:1)
我找到了答案。
替换这个:
var sqlCommand = new SqlCommand("Insert into JN_NewsCategories(NewsCategoriesFa,NewsCategoriesEn,IsGetNews)Values(@1,@2,@3)");
用这个:
var sqlCommand = new SqlCommand("Insert into JN_NewsCategories(NewsCategoriesFa,NewsCategoriesEn,IsGetNews)Values(@1,@2,@3)",sqlConnection);
答案 1 :(得分:1)
此问题是由于您的数据库连接或与数据库相关的事情, 与Quartz无关。
答案 2 :(得分:0)
这不是一个问题它告诉他无法找到石英的pdb来克服这个你有2个选择: