博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
using SSIS script task to send email result
阅读量:4876 次
发布时间:2019-06-11

本文共 3169 字,大约阅读时间需要 10 分钟。

sometimes notification email is required to be sent so that receivers can know about the data load status. Following C# code in SSIS script task is composed to meet the requirement.

1. drag a SQL task to get data result and assigned full set to an object variable(e.g.oCompleteFileList)

2. drag a script task to compose HTML message body and send email

 

using System;

using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Net.Mail;

 

 

 public void Main()

        {
            // TODO: Add your code here

            //Variable list

            //User::ccSeller,User::sEmailFrom,User::nFileCount,User::oCompleteFileList,User::sEmailToList,User::Environment
            string vSeller = Dts.Variables["ccSeller"].Value.ToString();
            string vEmailFrom = Dts.Variables["sEmailFrom"].Value.ToString();
            string vEmailTo = Dts.Variables["sEmailToList"].Value.ToString();
            object vFileList = Dts.Variables["oCompleteFileList"].Value;
            int vFileCount = (int)Dts.Variables["nFileCount"].Value;
            string vEnv = Dts.Variables["Environment"].Value.ToString();

            //setup smtp connection

            SmtpClient vSmtp = new SmtpClient();
            string vSmtpcm = Dts.Connections["SMTP Connection Manager"].ConnectionString.ToString();
            vSmtp.Host = (vSmtpcm.Split(';')[0]).Split('=')[1];
            vSmtp.UseDefaultCredentials = true;
            MailMessage vmsg = new MailMessage();
            //send from
            vmsg.From = new MailAddress(vEmailFrom);
            //send to
            Array vToList = vEmailTo.Split(';');
            foreach (string s in vToList)
            {
                vmsg.To.Add(new MailAddress(s));
            }
            //message subject and message body
            vmsg.IsBodyHtml = true;
            OleDbDataAdapter oleDA = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            string rc = "", msgxml = "", hd = "";
            string newline = Environment.NewLine;
            string blank4 = "    ", blank1 = " ";

            if (vFileCount == 0)

            {
                msgxml = "No files were loaded";
                vmsg.Subject =vEnv + ":" + vSeller + " - No files were loaded.";
            }
            else
            {
                //read sql result
                oleDA.Fill(dt, vFileList);
                //compose table header
                foreach (DataColumn col in dt.Columns)
                {
                    hd = hd + "<th style='border:1px solid black'>" + col.ColumnName + "</th>";
                }
                hd = "<tr style='background-color:#4F81BD;color:white'>" + hd + "</tr>" + newline;
                //compose table content
                foreach (DataRow row in dt.Rows)
                {
                    rc = "";
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (col.Ordinal != dt.Columns.Count - 1)
                        {
                            rc = rc + "<td style='border:1px solid'>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                        }
                        else
                        {
                            if (row[col.Ordinal].ToString().ToUpper() == "SUCCESS")
                            {
                                rc = rc + "<td style='border:1px solid;background-color:green'>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                            }
                            else
                            {
                                rc = rc + "<td style='border:1px solid;background-color:red'>" + blank1 + row[col.Ordinal].ToString() + blank4 + "</td>";
                            }
                        }
                    }
                    msgxml = msgxml + "<tr>" + rc + "</tr>" + newline;
                }
                //compose final xml
                msgxml = "<table cellspacing=50 style='border:1px solid;border-collapse:collapse'>" + newline + hd + msgxml + "</table>";
                vmsg.Subject = vEnv + ":" + vSeller + " - Detail loaded files list";
            }
            vmsg.Body = msgxml;
            //send email
            vSmtp.Send(vmsg);

            Dts.TaskResult = (int)ScriptResults.Success;

        }
    }

转载于:https://www.cnblogs.com/hopecho/p/4167109.html

你可能感兴趣的文章
代码生成器Sql Server 和 Mysql 数据库脚本
查看>>
重温PHP之快速排序
查看>>
PF部分代码解读
查看>>
ACM 新手入门 之 如何实现多组输入输出
查看>>
iOS中UI阶段常用的一些方法
查看>>
雾雨魔理沙 (Standard IO)
查看>>
[APIO2012] 派遣
查看>>
批处理命令 - for
查看>>
验证字符串是否为有效的IP地址
查看>>
CentOS 安装开发工具包
查看>>
Access 用sql语句添加自增列
查看>>
MySQL 主键外键
查看>>
jenkins-01初识jenkins
查看>>
MAPZONE GIS SDK接入Openlayers3之一——矢量数据集接入
查看>>
Ubuntu完全使用文档
查看>>
Django实战(23):权限控制
查看>>
除了《一无所有》,我一无所有
查看>>
[译]Atomic VS. Non-Atomic 操作
查看>>
BZOJ1303 [CQOI2009]中位数图 【乱搞】
查看>>
BZOJ4199 [Noi2015]品酒大会 【后缀数组 + 单调栈 + ST表】
查看>>