sql-server – 如何使用SSIS将查询结果集作为附件发送到电子邮件
我有一个保存在System.Object变量上的执行SQL任务查询的结果集,我想使用MessageSource上的以下表达式使用发送邮件任务发送此结果.
解决方法可能你现在已经找到了问题的答案.这个答案适用于可能偶然发现这个问题的其他人.我不认为你可以在表达式中使用对象变量.您需要遍历查询结果对象并将其格式化为字符串,以便您可以在电子邮件中发送查询输出.您还可以将数据导出到文件并将文件作为附件发送.这是另一种可能的选择.此示例显示如何遍历查询结果集以形成将使用“发送电子邮件”任务通过电子邮件发送的邮件正文.分步过程: >使用SQL Scripts部分下提供的脚本创建名为dbo.EmailData的表. 希望有所帮助. SQL脚本: CREATE TABLE [dbo].[EmailData]( [Id] [int] IDENTITY(1,1) NOT NULL,[ItemId] [varchar](255) NOT NULL,[ItemName] [varchar](255) NOT NULL,[ItemType] [varchar](255) NOT NULL,[IsProcessed] [bit] NULL,CONSTRAINT [PK_EmailData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO 脚本任务代码: 只能在SSIS 2008及更高版本中使用的C#代码. /*Microsoft SQL Server Integration Services Script Task Write scripts using Microsoft Visual C# 2008. The ScriptMain is the entry point class of the script. */ using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; namespace ST_7f59d09774914001b60a99a90809d5c5.csproj { [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 public void Main() { Variables varCollection = null; string header = string.Empty; string message = string.Empty; Dts.VariableDispenser.LockForWrite("User::EmailMessage"); Dts.VariableDispenser.LockForWrite("User::ItemId"); Dts.VariableDispenser.LockForWrite("User::ItemName"); Dts.VariableDispenser.LockForWrite("User::ItemType"); Dts.VariableDispenser.GetVariables(ref varCollection); //Set the header message for the query result if (varCollection["User::EmailMessage"].Value == string.Empty) { header = "Execute SQL task output sent using Send Email Task in SSIS:nn"; header += string.Format("{0}t{1}ttt{2}n","Item number","Item name","Item type"); varCollection["User::EmailMessage"].Value = header; } //Format the query result with tab delimiters message = string.Format("{0}t{1}t{2}",varCollection["User::ItemId"].Value,varCollection["User::ItemName"].Value,varCollection["User::ItemType"].Value); varCollection["User::EmailMessage"].Value = varCollection["User::EmailMessage"].Value + message; Dts.TaskResult = (int)ScriptResults.Success; } } } 截图#1: 截图#2: 截图#3: 截图#4: 截图#5: 截图#6: 截图#7: 截图#8: 截图#9: 截图#10: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |