用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

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

2016-08-14 作者: 小章举报

[c#]代码库

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包运行状态检查";
 
        /// <summary>
        /// 每日发送邮件的整点
        /// </summary>
        private readonly int clock = 8;
 
        /// <summary>
        /// 连接字符串
        /// </summary>
        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;
        }
 
        /// <summary>
        /// 检查ERP数据库的SSIS包的运行状态,并在失败或正在运行时发送邮件给客户
        /// </summary>
        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;
            }
        }
 
        /// <summary>
        /// 发送通知邮件
        /// </summary>
        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);
            }
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...