c# - 如何在 C# 中监视 SQL Server 代理作业信息

标签 c# sql-server-2000 sql-server-agent

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

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

你建议怎么做。

开发者工具:

  1. MS SQL Server 2000 SP4
  2. MS Visual Studio 2008(C#)

我是一名菜鸟程序员。

最佳答案

我已经可以做到了...

我在数据库“msdb”中选择表单表“Sysjobserver”以获取我想要的作业的读取状态、日期和时间。

使用此代码

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

关于c# - 如何在 C# 中监视 SQL Server 代理作业信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3757335/

相关文章:

sql-server-2000 - SQL 2000 : T-SQL to get foreign key relationships for a table

sql - 我可以根据表中的值返回一个字符串吗?

sql-server - Messenger 服务尚未启动 - NetSend 通知将不会发送

sql-server-agent - 每次我启动 SQL Server 代理作业时,我都会收到错误日志

sql-server - 如何使用输出和退出代码运行 SQL Server 代理 Powershell 脚本

c# - SecureString 在 C# 应用程序中实用吗?

c# - 使用 Asp.net 连接 MySql 时出错

sql-server - SQL Server : What locale should be used to format numeric values into SQL Server format?

c# - 我们已将 VB6 代码迁移到 .net 中的 C#

c# - 模式匹配变量范围