在SSIS中,如何以表格的形式从对象和邮件中读取数据

时间:2016-10-20 04:26:26

标签: ssis

我的主要目标是:

  1. 读取我通过执行Sql任务

  2. 获得的对象
  3. 然后使用脚本任务我想邮寄它,但我需要以表格的形式格式化对象中的值作为邮件正文。

  4. 并且IF Time TimeDifference列的值=< 100然后Row应该是Green Redse Red。

  5. enter image description here

    所以我有一个名为" ResultSet"的对象,我传递给Script Task,我把它转换成c#表结构并将它放在一个名为" ApplicationTotal"的变量中。 以下是可以正常使用的代码。

    public void Main()
        {
            DataTable dtTotal = new DataTable();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataTable dt = new DataTable();
    
            adapter.Fill(dt, Dts.Variables["InactiveSet"].Value);
    
            // In the first Run dtTotal is created
            if (Convert.ToInt32(Dts.Variables["InsertedRowCountTotal"].Value) == 0)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                   dtTotal.Columns.Add(dc.ColumnName,dc.DataType);
                }
            }
            else // In the next runs dtTotal is retrieved from variable
            {
                dtTotal = (DataTable)Dts.Variables["InactiveSetTotal"].Value;
            }
    
            foreach (DataRow dr in dt.Rows)
            {
                DataRow newDR = dtTotal.NewRow();
    
                foreach( DataColumn dc in dt.Columns)
                {
                    newDR[dc.ColumnName] = dr[dc.ColumnName];
                }
    
                dtTotal.Rows.Add(newDR);
            }
    
            Dts.Variables["InactiveSetTotal"].Value = dtTotal;
            Dts.Variables["InsertedRowCountTotal"].Value = Convert.ToInt32(Dts.Variables["InsertedRowCountTotal"].Value)  + Convert.ToInt32(Dts.Variables["InsertedRowCount"].Value);
    
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    

    然后我将ApplicationTotal对象传递给脚本任务,我必须读取对象并以表格的形式邮寄详细信息。我成功发送邮件,但我无法格式化表格中的数据并更改颜色

    我把邮件输出作为

    enter image description here

            if (Convert.ToInt32(Dts.Variables["InsertedRowCount"].Value) == 0)
            {
                return;
            }
    
    
            #region BuildingEmailBody
    
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("Monitor Application Report"));
            sb.AppendLine();
            sb.AppendLine();
            //sb.AppendLine(string.Format("Following are the Details:\n\n<TABLE><TR>\n<TH>{0}</TH><TH>{1}</TH><TH>{2}</TH> <TH>{3}</TH> <TH>{4}</TH>\n</TR>\n", "TimeDifferences(Minutes) ", "UpdateTime", "ApplicationName", "ServerName", "DatabaseName"));
    
            OleDbDataAdapter adapter = new OleDbDataAdapter();
    
            if (Convert.ToInt32(Dts.Variables["InsertedRowCount"].Value) > 0)
            {
                sb.AppendLine();
                DataTable ApplicationTotal = new DataTable();
                ApplicationTotal = (DataTable)Dts.Variables["ApplicationTotal"].Value;
    
                foreach (DataRow dr in ApplicationTotal.Rows)
                {
                    sb.AppendLine(string.Format("{0}     {1}      {2}      {3}        {4}  ", dr[0], dr[1], dr[2], dr[3], dr[4]));
                }
    
    
    
    
                sb.AppendLine();
            }
    
    
            # endregion
    

1 个答案:

答案 0 :(得分:0)

在上面的代码中,

1.我已经采用了一个变量html,我在其中创建表并在表中加载数据。

2.我根据条件

将颜色编码添加到表格的行中

3.Make IsBodyHtml = true

4.Pass html变量为smpt.Send(message)

 public void Main()
    {
        string html = string.Empty;
       if (Convert.ToInt32(Dts.Variables["InsertedRowCount"].Value) == 0)
        {
            return;
        }


        #region BuildingEmailBody


        OleDbDataAdapter adapter = new OleDbDataAdapter();

        if (Convert.ToInt32(Dts.Variables["InsertedRowCount"].Value) > 0)
        {
            DataTable ApplicationTotal = new DataTable();
            ApplicationTotal = (DataTable)Dts.Variables["ApplicationTotal"].Value;


            html = "Monitor Application Report <br></br>  <style type='text/css'>td.datacellone {   background-color: #FF0000; color: black;}td.datacelltwo {   background-color:   #00FF00; color: black;}</style> <table border=1>";
            //add header row
            html += "<tr>";
            for (int i = 0; i < ApplicationTotal.Columns.Count; i++)
            {
                html += "<th>" + ApplicationTotal.Columns[i].ColumnName + "</th>";
            }
            html += "</tr>";
            //add rows
            for (int i = 0; i < ApplicationTotal.Rows.Count; i++)
            {
                html += "<tr>";
                for (int j = 0; j < ApplicationTotal.Columns.Count; j++)
                    if (Convert.ToInt32(ApplicationTotal.Rows[i][0].ToString()) > Convert.ToInt32(Dts.Variables["TimeDifference"].Value.ToString()))
                       html += "<td class='datacellone'>" + ApplicationTotal.Rows[i][j].ToString() + "</td>";
                  else
                        html += "<td class='datacelltwo'>" + ApplicationTotal.Rows[i][j].ToString() + "</td>";


                html += "</tr>";
            }
            html += "</table>";


        }

        //MessageBox.Show(Dts.Variables["TimeDifference"].Value.ToString());
        # endregion

        #region SendingEmail

        string sendTo = Dts.Variables["AlarmOperator"].Value.ToString();
        string from = "pemsadmin@pemsportal.com.au";
        string subject = "Monitor Application Status";

        string server = "192.168.240.171";
        string user = "pemsadmin@pemsportal.com.au";
        string password = "Sawu7619";
        string domain = "pemsportal.com.au";
        int port = 25;

        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, sendTo, subject, html.ToString());

        message.IsBodyHtml = true;
        message.Body = html.ToString();
        System.Net.Mail.SmtpClient smpt = new System.Net.Mail.SmtpClient(server, port);
        smpt.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        smpt.Credentials = new System.Net.NetworkCredential(user, password, domain);

        smpt.Send(message);

        #endregion


        Dts.TaskResult = (int)ScriptResults.Success;
    }