2010-09-21 95 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); 
     } 
    } 
} 

}