SSIS 脚本任务 - 向多个收件人发送不同的结果
SSIS script task - send different results to multiple recipients
我很难将不同的正文发送给多个收件人。
这是我正在尝试做的事情:
我有一个正在执行的 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”。
如果您有任何问题,请告诉我。期待建议和答案!谢谢!
要考虑的一个选项是,使用 DataTable
的 Select
方法查找匹配的电子邮件记录以在您的内部循环中处理。
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);
}
我很难将不同的正文发送给多个收件人。
这是我正在尝试做的事情:
我有一个正在执行的 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”。
如果您有任何问题,请告诉我。期待建议和答案!谢谢!
要考虑的一个选项是,使用 DataTable
的 Select
方法查找匹配的电子邮件记录以在您的内部循环中处理。
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);
}