Running a SQL Job from .Net C# Windows Form

  • 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!

  • nevermind, I just realized I was missing: ExecJob.Connection = cn;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply