[C#] 每日检查SQL Server 2012数据库Job运行状态,并发送邮件 →→→→→进入此内容的聊天室

来自 , 2019-12-13, 写在 C#, 查看 107 次.
URL http://www.code666.cn/view/0e900ad8
  1. using Microsoft.SqlServer.Management.Smo.Agent;
  2. using System;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Windows.Forms;
  6.  
  7. namespace CheckDatabaseJob
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         private readonly string emailTemplatePath = Environment.CurrentDirectory + @"\通知邮件.oft";
  17.  
  18.         private BackgroundWorker checkSQLServerStatusWorker = new BackgroundWorker();
  19.  
  20.         private readonly string formOriginalTitle = "每日ERP数据库的SSIS包运行状态检查";
  21.  
  22.         /// <summary>
  23.         /// 每日发送邮件的整点
  24.         /// </summary>
  25.         private readonly int clock = 8;
  26.  
  27.         /// <summary>
  28.         /// 连接字符串
  29.         /// </summary>
  30.         private readonly string connectionString = @"Data Source=(local);Integrated Security=True;";
  31.  
  32.         private string formTitle;
  33.  
  34.         public bool forceStart = false;
  35.  
  36.         private void CheckERPJobStatusComplete(object sender, RunWorkerCompletedEventArgs eventArgs)
  37.         {
  38.             this.Text = formTitle;
  39.         }
  40.  
  41.         /// <summary>
  42.         /// 检查ERP数据库的SSIS包的运行状态,并在失败或正在运行时发送邮件给客户
  43.         /// </summary>
  44.         private void CheckERPJobStatusAndSendEmail(object sender, DoWorkEventArgs eventArgs)
  45.         {
  46.             try
  47.             {
  48.                 SMO smo = new SMO(connectionString);
  49.                 smo.JobName = "ERP_ETL";
  50.                 DateTime dt = smo.ServerJobServerJobLastRunDate;
  51.                 CompletionResult result = smo.ServerJobServerJobLastRunOutcome;
  52.                 if (!dt.Date.Equals(DateTime.Today) || result != CompletionResult.Succeeded)
  53.                     SendNotificationEmail();
  54.                 if (forceStart)
  55.                     forceStart = false;
  56.                 formTitle = formOriginalTitle;
  57.             }
  58.             catch (Exception ex)
  59.             {
  60.                 formTitle = formOriginalTitle + " - " + ex.Message + " - " + DateTime.Now.ToString("yyyy/M/dd hh:mm:ss");
  61.                 forceStart = true;
  62.                 timer2.Interval = 60000;
  63.                 timer2.Enabled = true;
  64.             }
  65.         }
  66.  
  67.         /// <summary>
  68.         /// 发送通知邮件
  69.         /// </summary>
  70.         private void SendNotificationEmail()
  71.         {
  72.             var app = new Microsoft.Office.Interop.Outlook.Application();
  73.             var mail = app.CreateItemFromTemplate(emailTemplatePath);
  74.             mail.HTMLBody = mail.HTMLBody.ToString().Replace("{0}", DateTime.Today.ToString("yyyy.M.dd"));
  75.             mail.Send();
  76.         }
  77.  
  78.         private void chkERPJobStatusAndSendEmail_CheckedChanged(object sender, EventArgs e)
  79.         {
  80.             timer1.Enabled = false;
  81.             timer2.Enabled = false;
  82.             if (chkERPJobStatusAndSendEmail.Checked)
  83.                 CheckIfRunWorker();
  84.         }
  85.  
  86.         private void CheckIfRunWorker()
  87.         {
  88.             if (DateTime.Now.Minute == 0)
  89.                 timer1.Enabled = true;
  90.             else
  91.             {
  92.                 timer2.Interval = ((60 - DateTime.Now.Minute) * 60 - DateTime.Now.Second) * 1000;
  93.                 timer2.Enabled = true;
  94.             }
  95.         }
  96.  
  97.         private void timer1_Tick(object sender, EventArgs e)
  98.         {
  99.             if (DateTime.Now.Hour == clock)
  100.                 checkSQLServerStatusWorker.RunWorkerAsync();
  101.         }
  102.  
  103.         private void Form1_Load(object sender, EventArgs e)
  104.         {
  105.             if (!File.Exists(emailTemplatePath))
  106.             {
  107.                 MessageBox.Show(string.Format("发送通知邮件模板({0})不存在, 请检查!", emailTemplatePath));
  108.                 Application.Exit();
  109.             }
  110.             else
  111.             {
  112.                 CheckIfRunWorker();
  113.                 checkSQLServerStatusWorker.DoWork += CheckERPJobStatusAndSendEmail;
  114.                 checkSQLServerStatusWorker.RunWorkerCompleted += CheckERPJobStatusComplete;
  115.             }
  116.         }
  117.  
  118.         private void timer2_Tick(object sender, EventArgs e)
  119.         {
  120.             timer2.Enabled = false;
  121.             if (chkERPJobStatusAndSendEmail.Checked && (DateTime.Now.Hour == clock || forceStart))
  122.                 checkSQLServerStatusWorker.RunWorkerAsync();
  123.             if (!forceStart)
  124.                 timer1.Enabled = chkERPJobStatusAndSendEmail.Checked;
  125.         }
  126.  
  127.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  128.         {
  129.             if (this.WindowState == FormWindowState.Minimized)
  130.             {
  131.                 this.Show();
  132.                 this.WindowState = FormWindowState.Normal;
  133.                 notifyIcon1.Visible = false;
  134.                 this.ShowInTaskbar = true;
  135.             }
  136.         }
  137.  
  138.         private void Form1_SizeChanged(object sender, EventArgs e)
  139.         {
  140.             if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
  141.             {
  142.                 this.ShowInTaskbar = false; //不显示在系统任务栏
  143.                 notifyIcon1.Visible = true; //托盘图标可见
  144.                 notifyIcon1.ShowBalloonTip(1000);
  145.             }
  146.         }
  147.     }
  148. }

回复 "每日检查SQL Server 2012数据库Job运行状态,并发送邮件"

这儿你可以回复上面这条便签

captcha