Hello,
I would like to have a button when pushed trigger a SQL Job run. Below is my code, for some reason I get an error message that says, "ExecuteNonQuery: Connection property has not been initialized.".
Does anyone have any suggestions?
private void btnRunDataRefresh_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(WindowsFormsApplication1.Properties.Settings.Default.ConnectionString);
SqlCommand ExecJob = new SqlCommand();
ExecJob.CommandType = CommandType.StoredProcedure;
ExecJob.CommandText = "msdb.dbo.sp_start_job";
ExecJob.Parameters.AddWithValue("@job_name", "Job_Name");
using (cn)
{
cn.Open();
using (ExecJob)
{
ExecJob.ExecuteNonQuery();
}
}
Thank you in advance!