Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从sql Server到textbox获取作业名称和状态,以遵循时间计划_C#_Sql Server - Fatal编程技术网

C# 如何从sql Server到textbox获取作业名称和状态,以遵循时间计划

C# 如何从sql Server到textbox获取作业名称和状态,以遵循时间计划,c#,sql-server,C#,Sql Server,现在,我可以通过连接字符串连接MS SQL Server 2000并获取作业名称和作业 SQL Server代理中的状态。但我想随着时间的推移不断显示姓名和身份 计划监视作业的重复周期。请建议如何做。使用MS Visual 工作室2008 C public void ConnectJOB() { SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass(); try {

现在,我可以通过连接字符串连接MS SQL Server 2000并获取作业名称和作业

SQL Server代理中的状态。但我想随着时间的推移不断显示姓名和身份

计划监视作业的重复周期。请建议如何做。使用MS Visual

工作室2008 C

public  void ConnectJOB() 
    {
        SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
        try
        {
            SQLServer.Connect("10.17.13.70", "sa", "");
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        SQLDMO.JobServer jobServer = SQLServer.JobServer;
        foreach (SQLDMO.Job job in jobServer.Jobs)
        {
            string jobName = job.Name;
            SQLDMO.SQLDMO_JOBEXECUTION_STATUS status = job.CurrentRunStatus;


            richTextBox1.Text = jobName.ToString();
            richTextBox2.Text = status.ToString();
        }

您可以使用直接的T-SQL语句从msdb数据库中的sysjobs和sysjobhistory表中获取有关作业及其历史状态的信息,如下所示:

public class JobInfo
{
    public string Name { get; set; }
    public Guid ID { get; set; }
    public byte Enabled { get; set; }
    public int Status { get; set; }
}

public List<JobInfo> GetJobsAndStatus()
{
   List<JobInfo> _jobs = new List<JobInfo>();

   string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_state " +
                        "from sysjobs j inner join sysjobhistory jh on j.job_id = jh.job_id";

   // create SQL connection and set up SQL Command for query
   using(SqlConnection _con = new SqlConnection("server=10.17.13.70;database=msdb;user id=sa;pwd=XXXXXXX"))
   using(SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
   {
       // open connection
       _con.Open();

       // create SQL Data Reader and grab data
       using(SqlDataReader rdr = _cmd.ExecuteReader())
       {
           // as long as we get information from the reader
           while(rdr.Read())
           {
               Guid jobID = rdr.GetGuid(0);        // read Job_id
               string jobName = rdr.GetString(1);  // read Job name
               byte jobEnabled = rdr.GetByte(2);   // read Job enabled flag
               int jobStatus = rdr.GetInt32(3);    // read run_state from jobhistory

               _jobs.Add(new JobInfo { Name = jobName, 
                                       ID = jobID, 
                                       Enabled = jobEnabled,
                                       Status = jobStatus });
    }

    rdr.Close();
}

// close connection again
_con.Close();     

return _jobs;
}

一旦有了作业及其历史记录/状态列表,您就可以随心所欲地使用它,例如,抓取第一个作业并将值放入文本框中,或者任何您想要的内容。

您可能需要在SQL Server 2000中签出SQL Server Agent作业系统表并从中获取信息

有关可用表格的概述,请查看此。主表是sysjobs和sysjobhistory,您可以检查并找到所需的信息。需要注意的一点是:这些表位于msdb数据库中,您需要相当高的权限才能读取这些表

此查询将返回系统中当前的所有作业及其作业ID GUID、名称和启用状态:

使用该信息,并与sysjobhistory或其他相关表连接,以获取您要查找的信息

更新:

好的,这是完整的代码序列

// define the query to run
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status " +
                     "from sysjobs j inner join sysjobhistory jh on j.job_id = jh.jobid"

// create SQL connection and set up SQL Command for query
using(SqlConnection _con = new SqlConnection("server=10.17.13.70;database=msdb;user id=sa;pwd=XXXXXX")
using(SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
    // open connection
    _con.Open();

    // create SQL Data Reader and grab data
    using(SqlDataReader rdr = _cmd.ExecuteReader())
    {
        // as long as we get information from the reader
        while(rdr.Read())
        {
            Guid jobID = rdr.GetGuid(0);        // read Job_id
            string jobName = rdr.GetString(1);  // read Job name
            int jobEnabled = rdr.GetInt(2);     // read Job enabled flag
            int jobRunStatus = rdr.GetInt(3);   // read job run status

            // do something with your data, e.g. store it into a list or something
        }

        rdr.Close();
    }

    // close connection again
    _con.Close();     
}

我认为最好在SQLServer中使用定时存储过程,并尝试获取作业列表并定期将它们写入文件中。然后您可以监视该文件。这只是个想法你到底想做什么?我理解你的问题,你想写一个工作监视器,是吗?是的,我想写一个工作监视器。我不明白。对不起,我是新手。谢谢你,我已经测试了你的代码,我做了一点申请。结果没有达到预期,我不知道该怎么办。
// define the query to run
string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status " +
                     "from sysjobs j inner join sysjobhistory jh on j.job_id = jh.jobid"

// create SQL connection and set up SQL Command for query
using(SqlConnection _con = new SqlConnection("server=10.17.13.70;database=msdb;user id=sa;pwd=XXXXXX")
using(SqlCommand _cmd = new SqlCommand(sqlJobQuery, _con))
{
    // open connection
    _con.Open();

    // create SQL Data Reader and grab data
    using(SqlDataReader rdr = _cmd.ExecuteReader())
    {
        // as long as we get information from the reader
        while(rdr.Read())
        {
            Guid jobID = rdr.GetGuid(0);        // read Job_id
            string jobName = rdr.GetString(1);  // read Job name
            int jobEnabled = rdr.GetInt(2);     // read Job enabled flag
            int jobRunStatus = rdr.GetInt(3);   // read job run status

            // do something with your data, e.g. store it into a list or something
        }

        rdr.Close();
    }

    // close connection again
    _con.Close();     
}