Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Running a SQL Job from .Net C# Windows Form Expand / Collapse
Author
Message
Posted Wednesday, March 19, 2014 8:18 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 1:06 PM
Points: 69, Visits: 170
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!
Post #1552632
Posted Wednesday, March 19, 2014 8:51 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 1:06 PM
Points: 69, Visits: 170
nevermind, I just realized I was missing: ExecJob.Connection = cn;
Post #1552652
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse