我的要求是,如果我的包中使用的任何一个组件出现故障,我需要发送一封包含错误详细信息的电子邮件。
尝试使用谷歌搜索并阅读 ssis 中的优先约束和事件处理程序,但我的包很大并且有很多组件(控制流项)。
我们可以为包中使用的每个可执行文件定义许多事件处理程序吗?如果是这样,我认为为每个组件定义事件处理程序并不是一个好的性能。
请建议最好的方法来做到这一点......
我的要求是,如果我的包中使用的任何一个组件出现故障,我需要发送一封包含错误详细信息的电子邮件。
尝试使用谷歌搜索并阅读 ssis 中的优先约束和事件处理程序,但我的包很大并且有很多组件(控制流项)。
我们可以为包中使用的每个可执行文件定义许多事件处理程序吗?如果是这样,我认为为每个组件定义事件处理程序并不是一个好的性能。
请建议最好的方法来做到这一点......
您可以使用脚本任务在 SSIS 包中发送您自己的自定义电子邮件
在此您需要将配置读取 witter 变量设置为参数以发送自定义电子邮件
编辑脚本并将此代码写入脚本
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net.Mail;
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
#region VSTA generated code
/*enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};*/
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
string sSubject = "Integration Task Result";
int iPriority = 2;
if (SendMail(sSubject, iPriority))
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
//Fails the Task
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
public bool SendMail(string sSubject,int iPriority)
{
try
{
string Email_Server = Dts.Variables["Email_Server"].Value.ToString();
string Email_Port = Dts.Variables["Email_Port"].Value.ToString();
string Form_Email_User = Dts.Variables["Form_Email_User"].Value.ToString();
string From_Pass = Dts.Variables["From_Pass"].Value.ToString();
string To_Email = Dts.Variables["To_Email"].Value.ToString();
string CC_Email = Dts.Variables["CC_Email"].Value.ToString();
string From_Email = Dts.Variables["From_Email"].Value.ToString();
string Email_FromName = Dts.Variables["Email_FromName"].Value.ToString();
string TaskID = Dts.Variables["TaskID"].Value.ToString();
string TaskName = Dts.Variables["TaskName"].Value.ToString();
string FailedConfigurations = Dts.Variables["FailedConfigurations"].Value.ToString();
string PackageName = Dts.Variables["PackageName"].Value.ToString();
string StartTime = Dts.Variables["StartTime"].Value.ToString();
string CreatorComputerName = Dts.Variables["CreatorComputerName"].Value.ToString();
string UserName = Dts.Variables["UserName"].Value.ToString();
String sBody = "StartTime = " + StartTime +
"<br/>TaskID = " + TaskID +
"<br/>TaskName = " + TaskName +
"<br/>UserName = " + UserName +
"<br/>CreatorComputerName = " + CreatorComputerName +
"<br/>PackageName = " + PackageName;
if (FailedConfigurations.Equals(""))
{
sBody = sBody + "<br/><br/>Task Execution failure";
} else {
sBody = sBody + "<br/><br/>FailedConfigurations = " + FailedConfigurations;
}
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(From_Email, Email_FromName);
//You can have multiple emails separated by ;
string[] sEmailTo = Regex.Split(To_Email, ";");
string[] sEmailCC = Regex.Split(CC_Email, ";");
int sEmailServerSMTP = int.Parse(Email_Port);
smtpClient.Host = Email_Server;
smtpClient.Port = sEmailServerSMTP;
smtpClient.EnableSsl = true;
System.Net.NetworkCredential myCredentials =
new System.Net.NetworkCredential(Form_Email_User, From_Pass);
smtpClient.Credentials = myCredentials;
message.From = fromAddress;
if (sEmailTo != null)
{
for (int i = 0; i < sEmailTo.Length; ++i)
{
if (sEmailTo[i] != null && sEmailTo[i] != "")
{
message.To.Add(sEmailTo[i]);
}
}
}
if (sEmailCC != null)
{
for (int i = 0; i < sEmailCC.Length; ++i)
{
if (sEmailCC[i] != null && sEmailCC[i] != "")
{
message.To.Add(sEmailCC[i]);
}
}
}
switch (iPriority)
{
case 1:
message.Priority = MailPriority.High;
break;
case 3:
message.Priority = MailPriority.Low;
break;
default:
message.Priority = MailPriority.Normal;
break;
}
message.Subject = sSubject;
message.IsBodyHtml = true;
message.Body = sBody;
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
使用事件处理程序选项卡来设置您希望在特定事件中发生的事情。您可以选择包中任何级别的事件(即跨整个包或仅在特定可执行文件上)。在事件处理程序中,添加一个发送电子邮件任务。