2010-09-21 97 views
6

当作业发生与Windows应用程序事件日志中的显示相同时,我需要创建一个用于监视SQL Server 2000代理作业状态和信息的应用程序。现在我已经通过连接字符串连接到数据库,但我不知道如何从Job获取状态和信息。如何在C#中监控SQL Server代理作业信息

我需要在文本框上显示状态和信息。

你建议怎么做。

开发工具:

  1. MS SQL Sever的2000 SP4
  2. MS Visual Studio 2008中(C#)

我是菜鸟程序员。

+3

你不会浪费别人的时间 - #1在这里特别要能够让社会各界的帮助! – 2010-09-21 04:52:39

+0

确定谢谢你,Stackoverflow非常好知识和帮助很多人^ _^ – Fernatit 2010-09-21 07:33:26

+0

请问可以选择正确的答案吗?直接使用SQL可以工作,或者SQLServer SMO C#库也可以工作。其中几个可以被接受为答案。 – DtechNet 2018-02-18 19:11:02

回答

2

这应该是一个很好的起点,以了解如何使用T-SQL来找到你的SQL Agent作业:

View (and disable) SQL Agent Jobs with TSQL

该脚本会列出您所有的工作在数据库上,当他们将运行下一步等等。

使用job_name,您还应该能够使用服务器上的msdb数据库中的SQL Server Agent Stored Procedures找到有关您的作业的详细信息。

+1

或者,看看这个MSDN页http://msdn.microsoft.com/en-us/library/aa260604%28v=SQL.80%29.aspx - 特别是SQL Server代理表。 – 2010-09-21 05:24:10

0

您可以使用此SELECT获取所有服务器任务的列表:

SELECT [name] FROM msdb.dbo.sysjobs 

如果您想获得当前正在运行的任务的列表和他们的信息,我建议写在SQL存储过程你的应用程序调用。这里有一个很好的示范,你可以使用...

http://feodorgeorgiev.com/blog/2010/03/how-to-query-currently-running-sql-server-agent-jobs/

祝你好运!

+0

如果您发布的是代码或XML,**请**在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码”按钮(101 010),以更好地格式化和语法突出显示它! – 2010-09-21 06:54:57

+0

感谢您的每一个建议,虽然我有点困惑,因为我是一个真正的新手,但我会尽力遵循每一个建议。 – Fernatit 2010-09-21 08:08:35

4

我能做到这一点已经...

我选择形式表“Sysjobserver”数据库“MSDB”阅读状态,日期,工作时间,我想。

使用此代码

public void GetJobsAndStatus() 
     { 
      string sqlJobQuery = "select j.job_id, j.name, j.enabled, jh.run_status," + 
      " js.last_outcome_message, jh.run_date, jh.step_name, jh.run_time" + 
      " from sysjobs j left join sysjobhistory jh on (j.job_id = jh.job_id)" + 
      " left join sysjobservers js on (j.job_id = js.job_id)" + 
      " where jh.run_date = (select Max(run_date) from sysjobhistory)" + 
      " and jh.run_time = (select Max(run_time) from sysjobhistory)"; 

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

      { 

       try 
       { 
       // open connection 
       _con.Open(); 
       SqlConnection.ClearPool(_con); 

       // 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 last_run_outcome from sysjobserver 
         string jobMessage = rdr.GetString(4); // read Message from sysjobserver 
         int jobRunDate = rdr.GetInt32(5);  // read run_date from sysjobhistory 
         string jobStepName = rdr.GetString(6); // read StepName from sysjobhistory 
         int jobRunTime = rdr.GetInt32(7);  // read run_time from sysjobhistory 


         String[] lviData = new String[] // ตัวแปรอะเรย์ชื่อ lviData 
        { 
         jobID.ToString(), 
         jobName.ToString(), 
         jobStepName.ToString(), 
         jobMessage.ToString(), 
         jobStatus.ToString(), 
         jobRunDate.ToString(), 
         jobRunTime.ToString(), 
         //jobEnabled.ToString(), 

        }; 

         newData = lviData; 

         DisplayList(); // for display data on datagridview 


        } 

        rdr.Close(); 
       } 
      } 

谢谢你为大家很大帮助。 :-D

+0

此查询需要msdb系统数据库上的db?拥有者权限,并非每个人都拥有该权限。通过具有专用角色的SQL Server Agent可以获取作业,并且不需要拥有系统数据库的所有者权限。 – 2012-05-15 16:38:06

1

On SQL Server 2005及更高版本,您可以使用系统存储过程msdb.dbo.sp_help_job来获取有关SQL Server代理作业的信息,包括状态。您可以通过http://msdn.microsoft.com/en-us/library/ms186722(v=SQL.90).aspx了解有关sp_help_job的更多信息。

这里是从C#做到这一点的示例代码。查询

private Dictionary<int, string> ExecutionStatusDictionary = new Dictionary<int, string>() 
{ 
    {0, "Not idle or suspended"}, 
    {1, "Executing"}, 
    {2, "Waiting for thread"}, 
    {3, "Between retries"}, 
    {4, "Idle"}, 
    {5, "Suspended"}, 
    {7, "Performing completion actions"} 
}; 

public string GetStatus() 
{ 
    SqlConnection msdbConnection = new SqlConnection("Data Source=GACDTL01CR585M;Initial Catalog=msdb;Integrated Security=SSPI"); 
    System.Text.StringBuilder resultBuilder = new System.Text.StringBuilder(); 

    try 
    { 
     msdbConnection.Open(); 

     SqlCommand jobStatusCommand = msdbConnection.CreateCommand(); 

     jobStatusCommand.CommandType = CommandType.StoredProcedure; 
     jobStatusCommand.CommandText = "sp_help_job"; 

     SqlParameter jobName = jobStatusCommand.Parameters.Add("@job_name", SqlDbType.VarChar); 
     jobName.Direction = ParameterDirection.Input; 
     jobName.Value = "LoadRegions"; 

     SqlParameter jobAspect = jobStatusCommand.Parameters.Add("@job_aspect", SqlDbType.VarChar); 
     jobAspect.Direction = ParameterDirection.Input; 
     jobAspect.Value = "JOB"; 

     SqlDataReader jobStatusReader = jobStatusCommand.ExecuteReader(); 

     while (jobStatusReader.Read()) 
     { 
      resultBuilder.Append(string.Format("{0} {1}", 
       jobStatusReader["name"].ToString(), 
       ExecutionStatusDictionary[(int)jobStatusReader["current_execution_status"]] 
      )); 
     } 
     jobStatusReader.Close(); 
    } 
    finally 
    { 
     msdbConnection.Close(); 
    } 

    return resultBuilder.ToString(); 
} 
3

SQL存储过程不会给你任何系统数据,除非你有msdb系统数据库上的db_owner权限,以在SQL Server 2008租赁提到所以通常方法不为应用程序在哪里工作想展示或管理工作。然而,SMO命名空间为您提供了许多SQL Server管理功能的托管代码解决方案,其中包括仅需要SQLServerAgent *权限的SQL Server代理函数,您通常可以根据您的应用程序用户对这些权限进行排序。使用SMO类与就业工作的一个很好的介绍,这里给出:

http://www.codeproject.com/Tips/367470/Manage-SQL-Server-Agent-Jobs-using-Csharp

我在一个类似的工作任务现在虽然SQL查询就给我拒绝访问,与C#代码和Microsoft.SqlServer.Management。 Smo.Agent命名空间我刚刚上市的所有作业,此代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.SqlServer.Management.Smo; 
using Microsoft.SqlServer.Management.Common; 
using Microsoft.SqlServer.Management.Smo.Agent; 

namespace SmoTest 
{ 
class Program 
{ 
    static readonly string SqlServer = @"SQL01\SQL01"; 

    static void Main(string[] args) 
    { 
     ServerConnection conn = new ServerConnection(SqlServer); 
     Server server = new Server(conn); 
     JobCollection jobs = server.JobServer.Jobs; 
     foreach (Job job in jobs) 
     { 
      Console.WriteLine(job.Name); 
     } 
    } 
} 

}