2009-09-14 55 views
0

我有這個使用SQL Server代理程序在服務器上以編程方式運行遠程程序包的小代碼。但是代碼的問題是,即使工作出現失敗,但工作成功執行。所以這個程序總是正常工作。c#中的SQL作業結果#

我想知道我是否真的可以在C#中捕獲工作結果..有什麼想法?

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(); 
} 

}

回答