在ASP.NET页上跟踪SQL Server作业的进度

时间:2018-12-19 14:42:43

标签: sql asp.net sql-server ssis

我有一个ASP.net页,该页上载Excel文件,然后执行SQL Server作业,该作业反过来启动SSIS包以将其导入。 我可以检查作业是启动还是启动失败,但是如何跟踪其进度,或者至少知道它是否已成功完成运行并将结果显示在ASP.net页面上?

我的代码如下:

 protected void ProcessData(int num)
    {
        SqlConnection jobConnection;
        SqlCommand jobCommand;
        SqlParameter jobReturnValue;
        SqlParameter jobParameter;
        int jobResult;

        //SSIS CONNECTION
        jobConnection = new SqlConnection("Data Source=.\\XX_XXX;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 = "RunXXX_Package0" + num;

        jobConnection.Open();
        jobCommand.ExecuteNonQuery();
        jobResult = (Int32)jobCommand.Parameters["@RETURN_VALUE"].Value;
        switch (jobResult)
        {
            case 0:
                Label2.Text = "SQL Server Agent job, RunXXX_Package0" + num + " started successfully.";
                break;
            default:
                Label2.Text = "SQL Server Agent job, RunXXX_Package0" + num + "  failed to start.";
                break;
        }
        jobConnection.Close();

    }

2 个答案:

答案 0 :(得分:1)

已将SSIS包的进度写到执行日志(或历史记录)表(您必须设计)中。

然后只需让您的ASP页读取该表即可。

答案 1 :(得分:0)

您可以通过民意调查[sp_help_job][1]来获取信息:

EXEC msdb.dbo.sp_help_job @Job_name = 'YourJobName'

然后检查 execution_status

0 - Returns only those jobs that are not idle or suspended.
1 - Executing.
2 - Waiting for thread.
3 - Between retries.
4 - Idle.
5 - Suspended.
7 - Performing completion actions.

last_run_outcome

0 = Failed
1 = Succeeded
3 = Canceled
5 = Unknown