using Microsoft.SqlServer.Management.Smo.Agent; using System; using System.ComponentModel; using System.IO; using System.Windows.Forms; namespace CheckDatabaseJob { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private readonly string emailTemplatePath = Environment.CurrentDirectory + @"\通知邮件.oft"; private BackgroundWorker checkSQLServerStatusWorker = new BackgroundWorker(); private readonly string formOriginalTitle = "每日ERP数据库的SSIS包运行状态检查"; /// /// 每日发送邮件的整点 /// private readonly int clock = 8; /// /// 连接字符串 /// private readonly string connectionString = @"Data Source=(local);Integrated Security=True;"; private string formTitle; public bool forceStart = false; private void CheckERPJobStatusComplete(object sender, RunWorkerCompletedEventArgs eventArgs) { this.Text = formTitle; } /// /// 检查ERP数据库的SSIS包的运行状态,并在失败或正在运行时发送邮件给客户 /// private void CheckERPJobStatusAndSendEmail(object sender, DoWorkEventArgs eventArgs) { try { SMO smo = new SMO(connectionString); smo.JobName = "ERP_ETL"; DateTime dt = smo.ServerJobServerJobLastRunDate; CompletionResult result = smo.ServerJobServerJobLastRunOutcome; if (!dt.Date.Equals(DateTime.Today) || result != CompletionResult.Succeeded) SendNotificationEmail(); if (forceStart) forceStart = false; formTitle = formOriginalTitle; } catch (Exception ex) { formTitle = formOriginalTitle + " - " + ex.Message + " - " + DateTime.Now.ToString("yyyy/M/dd hh:mm:ss"); forceStart = true; timer2.Interval = 60000; timer2.Enabled = true; } } /// /// 发送通知邮件 /// private void SendNotificationEmail() { var app = new Microsoft.Office.Interop.Outlook.Application(); var mail = app.CreateItemFromTemplate(emailTemplatePath); mail.HTMLBody = mail.HTMLBody.ToString().Replace("{0}", DateTime.Today.ToString("yyyy.M.dd")); mail.Send(); } private void chkERPJobStatusAndSendEmail_CheckedChanged(object sender, EventArgs e) { timer1.Enabled = false; timer2.Enabled = false; if (chkERPJobStatusAndSendEmail.Checked) CheckIfRunWorker(); } private void CheckIfRunWorker() { if (DateTime.Now.Minute == 0) timer1.Enabled = true; else { timer2.Interval = ((60 - DateTime.Now.Minute) * 60 - DateTime.Now.Second) * 1000; timer2.Enabled = true; } } private void timer1_Tick(object sender, EventArgs e) { if (DateTime.Now.Hour == clock) checkSQLServerStatusWorker.RunWorkerAsync(); } private void Form1_Load(object sender, EventArgs e) { if (!File.Exists(emailTemplatePath)) { MessageBox.Show(string.Format("发送通知邮件模板({0})不存在, 请检查!", emailTemplatePath)); Application.Exit(); } else { CheckIfRunWorker(); checkSQLServerStatusWorker.DoWork += CheckERPJobStatusAndSendEmail; checkSQLServerStatusWorker.RunWorkerCompleted += CheckERPJobStatusComplete; } } private void timer2_Tick(object sender, EventArgs e) { timer2.Enabled = false; if (chkERPJobStatusAndSendEmail.Checked && (DateTime.Now.Hour == clock || forceStart)) checkSQLServerStatusWorker.RunWorkerAsync(); if (!forceStart) timer1.Enabled = chkERPJobStatusAndSendEmail.Checked; } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; notifyIcon1.Visible = false; this.ShowInTaskbar = true; } } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) //判断是否最小化 { this.ShowInTaskbar = false; //不显示在系统任务栏 notifyIcon1.Visible = true; //托盘图标可见 notifyIcon1.ShowBalloonTip(1000); } } } }