views:

211

answers:

1

I have this small code using SQL Server Agent to Run a Remote Package Programmatically on the Server. But the problem with the code is that even if the job out come is failure, but job was executed successfully. so this program always works fine.

I want to know if I can actually capture the job result in C#.. Any thoughts?

namespace LaunchSSISPackageAgent_CS
{
  class Program
  {
static void Main(string[] args)
{
  SqlConnection jobConnection;
  SqlCommand jobCommand;
  SqlParameter jobReturnValue;
  SqlParameter jobParameter;
  int jobResult;

  jobConnection = new SqlConnection("Data Source=(local);Initial Catalog=msdb;Integrated Security=SSPI");
  jobCommand = new SqlCommand("sp_start_job", jobConnection);
  jobCommand.CommandType = CommandType.StoredProcedure;

  jobReturnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
  jobReturnValue.Direction = ParameterDirection.ReturnValue;
  jobCommand.Parameters.Add(jobReturnValue);

  jobParameter = new SqlParameter("@job_name", SqlDbType.VarChar);
  jobParameter.Direction = ParameterDirection.Input;
  jobCommand.Parameters.Add(jobParameter);
  jobParameter.Value = "RunSSISPackage";

  jobConnection.Open();
  jobCommand.ExecuteNonQuery();
  jobResult = (Int32)jobCommand.Parameters["@RETURN_VALUE"].Value;
  jobConnection.Close();

  switch (jobResult)
  {
    case 0:
      Console.WriteLine("SQL Server Agent job, RunSISSPackage, started successfully.");
      break;
    default:
      Console.WriteLine("SQL Server Agent job, RunSISSPackage, failed to start.");
      break;
  }
  Console.Read();
}

}

A: 

You could try running sp_help_job afterwards

http://technet.microsoft.com/en-us/library/ms186722%28SQL.90%29.aspx

Mike Blandford
There is no store proc. The job has 2 steps 1. execute a DTSX package that imports a ton of data from Excel 2. Executes couple of SQL statements after first step on the data.
Broken Link