【问题标题】:SSIS script task - send different results to multiple recipientsSSIS 脚本任务 - 向多个收件人发送不同的结果
【发布时间】:2021-03-23 19:23:26
【问题描述】:

我正在努力将不同的正文发送给多个收件人。

这是我想要做的:

我正在执行一个 SQL 查询,并在“执行 SQL 任务”中获取结果。

结果示例:

脚本如下所示:

public void Main()
    {
        // TODO: Add your code here
        Variables varCollection = null;
        Dts.VariableDispenser.LockForWrite("User::QueryResult");
        Dts.VariableDispenser.GetVariables(ref varCollection);
        string result = varCollection["User::QueryResult"].Value.ToString();
        var data = varCollection["User::QueryResult"].Value;
        

        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        da.Fill(dt, varCollection["User::QueryResult"].Value);

        foreach (DataRow row in dt.Rows)
        {
            string body = string.Empty;
            string emailTo = string.Empty;
            string subject = string.Empty;
            try
            {
                List<string> final = new List<string>();
                foreach (DataRow row2 in dt.Rows)
                {
                    emailTo = row2["Email"].ToString();
                    subject = row2["SalesOrder"].ToString() + " products have been approved";
                    List<string> temp = new List<string>();
                    if (row2[2].ToString() == emailTo)
                    {
                        temp.Add(row2.ToString());
                        final.Add(string.Join("\t", temp));
                    }
                    
                    body = string.Join("\r\n", final);
                }
                SendMailMessage("ssisnotifier@admin.com", emailTo, subject, body, true, credetnials);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

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

目前我得到的结果是完全不正确的。使用此脚本,所有接收者都将获得整个 DataTable。

我正在尝试将包含电子邮件“test.recepient@abc.com”的特定行发送到此电子邮件。

因此,基本上两行行将发送到“test.recepient@abc.com”,一行发送到“test2.recepient2@def.com”。

如果您有任何问题,请告诉我。期待建议和答案!谢谢!

【问题讨论】:

    标签: c# datatable ssis sendmail script-task


    【解决方案1】:

    要考虑的一个选项是,使用DataTableSelect 方法查找匹配的电子邮件记录以在您的内部循环中处理。

    public void Main()
    {
        // TODO: Add your code here
        Variables varCollection = null;
        Dts.VariableDispenser.LockForWrite("User::QueryResult");
        Dts.VariableDispenser.GetVariables(ref varCollection);
        string result = varCollection["User::QueryResult"].Value.ToString();
        var data = varCollection["User::QueryResult"].Value;
    
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        da.Fill(dt, varCollection["User::QueryResult"].Value);
    
        IList<string> workedEmails = new List<string>();
        foreach (DataRow row in dt.Rows)
        {
            string body = string.Empty;
            string emailTo = row[2].ToString().Trim();
            // already worked this email on a previous row; skip it this time
            if (workedEmails.Contains(emailTo)) continue;
            // filter rows for current email and use all matched rows to build message body
            string filter = $"Email = '{emailTo}'";
            DataRow[] matchingEmailRows = dt.Select(filter);
            foreach (DataRow matchedRow in matchingEmailRows)
            {
                string salesOrder = matchedRow[0].ToString();
                string subject = salesOrder + " products have been approved";
                string matchedEmail = matchedRow["Email"].ToString();
                if (emailTo == matchedEmail)
                {
                    body += subject + Environment.NewLine;
                }
            }
        SendMailMessage("ssisnotifier@admin.com", emailTo, subject, body, true, credetnials);
        workedEmails.Add(emailTo);
    }
    

    【讨论】:

    • 非常感谢@quaabaam,这成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2020-09-15
    相关资源
    最近更新 更多