在C#app中显示上次成功的SSIS作业的日期

时间:2015-10-07 21:41:16

标签: c# sql-server ssis

我正在通过代码更新以包含整个.aspx源代码,以便为那些帮助我输入代码的人提供更好的图片。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection maindb = new SqlConnection(My_Connection);

protected void Page_Load(object sender, EventArgs e)
{
    String str = "Select Statement";
    SqlCommand sc = new SqlCommand(str, maindb);

    maindb.Open();
    sc.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = sc;
    DataSet ds = new DataSet();
    da.Fill(ds, "FirstName");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    maindb.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
    String str = "Different Select Statement";
    SqlCommand sc = new SqlCommand(str, maindb);

    maindb.Open();
    sc.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = sc;
    DataSet ds = new DataSet();
    da.Fill(ds, "FirstName");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    maindb.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

我有一个每天运行的步骤作业,它会在SQL中更新几个表。

凭借我对C#的基本知识,我创建了一个应用程序,在gridview中显示存储在该SQL表中的数据。我想在应用程序上显示上一次成功的作业运行日期,类似于“Data Updated On”类型的东西。

在其他Stack Overflow问题上,我发现此代码在SQL管理工作室中显示了上一次成功作业的行。

DECLARE @job_id binary(16)
SELECT @job_id = job_id FROM msdb.dbo.sysjobs WHERE (name = N'YourJobName')
SELECT TOP 1
CONVERT(DATETIME, RTRIM(run_date))
+ ((run_time / 10000 * 3600) 
+ ((run_time % 10000) / 100 * 60) 
+ (run_time % 10000) % 100) / (86399.9964) AS run_datetime
, *
FROM
msdb..sysjobhistory sjh
WHERE
sjh.step_id = 0 
AND sjh.run_status = 1 
AND sjh.job_id = @job_id
ORDER BY
run_datetime DESC

我的问题是如何在实际应用中显示日期?

谢谢。

1 个答案:

答案 0 :(得分:1)

protected void Page_Load(object sender, EventArgs e)
{
    String str = "Select Statement";
    //Replace 'YourJobName' with the name of your SQL Job!
    string sqlSelect = @"DECLARE @job_id binary(16)
    SELECT @job_id = job_id FROM msdb.dbo.sysjobs WHERE (name = N'YourJobName')
    SELECT TOP 1
    CONVERT(DATETIME, RTRIM(run_date))
    + ((run_time / 10000 * 3600) 
    + ((run_time % 10000) / 100 * 60) 
    + (run_time % 10000) % 100) / (86399.9964) AS run_datetime
    , *
    FROM
    msdb..sysjobhistory sjh
    WHERE
    sjh.step_id = 0 
    AND sjh.run_status = 1 
    AND sjh.job_id = @job_id
    ORDER BY
    run_datetime DESC";

    using(var connection = new SqlConnection(My_Connection))
    {
        using (var sc = new SqlCommand(str, connection))
        {
            sc.ExecuteNonQuery();
            using (SqlDataAdapter da = new SqlDataAdapter() { SelectCommand = sc })
            {
                DataSet ds = new DataSet();
                da.Fill(ds, "FirstName");
                GridView1.DataSource = ds;
            }
            GridView1.DataBind();
        }

        using (var adapter = new SqlDataAdapter(sqlSelect, connection))
        {
            var table = new DataTable();
            adapter.Fill(table);
            Label1.Text = Convert.ToString(table.Rows[0][0]); 
        }
    }
}